2016-05-27 101 views
1

我必須根據inputArray和factorArray進行一些計算並創建第三個數組outputArray。你可以看到基於標的財產outputArray性標誌和noOfStudents caclucations部分基於Javascript中兩個對象數組計算的對象數組

var inputArray = [ 
    { subject: 'Maths', marks: '40', noOfStudents: '5' }, 
    { subject: 'Science', marks: '50', noOfStudents: '16' }, 
    { subject: 'History', marks: '35', noOfStudents: '23' }, 
    { subject: 'Science', marks: '65', noOfStudents: '2' }, 
    { subject: 'Maths', marks: '30', noOfStudents: '12' }, 
    { subject: 'History', marks: '55', noOfStudents: '20' } 
]; 

var factorArray = [ 
    { subject: 'History', marks: '500', noOfStudents: '200' }, 
    { subject: 'Maths', marks: '200', noOfStudents: '150' }, 
    { subject: 'Science', marks: '300', noOfStudents: '100' }  
]; 

var outputArray = [ 
    { subject: 'Science', marks: '300-(50+65)', noOfStudents: '100-(16+2)' }, 
    { subject: 'Maths', marks: '200-(40+30)', noOfStudents: '150-(5+12)' }, 
    { subject: 'History', marks: '500-(35+55)', noOfStudents: '200-(23+20)' } 
]; 

我需要的解決方案擴展到我剛纔的問題:https://stackoverflow.com/a/37481705

+0

是'subject'在'factorArray'獨特之處? – IMTheNachoMan

+2

那麼,你的問題是什麼?你堅持的是什麼?你做了什麼來試圖解決這個問題? –

+0

如果結果看起來像'marks:'300-(50 + 65)',或者你需要計算結果'marks:'185''嗎? –

回答

1

這一提案使用臨時對象,首先對inputArray進行分組的結果,然後再創建outputArray。該操作的複雜性是線性O(n + m)。

var inputArray = [{ subject: 'Maths', marks: '40', noOfStudents: '5' }, { subject: 'Science', marks: '50', noOfStudents: '16' }, { subject: 'History', marks: '35', noOfStudents: '23' }, { subject: 'Science', marks: '65', noOfStudents: '2' }, { subject: 'Maths', marks: '30', noOfStudents: '12' }, { subject: 'History', marks: '55', noOfStudents: '20' }], 
 
    factorArray = [{ subject: 'History', marks: '500', noOfStudents: '200' }, { subject: 'Maths', marks: '200', noOfStudents: '150' }, { subject: 'Science', marks: '300', noOfStudents: '100' }], 
 
    temp = Object.create(null), 
 
    outputArray = []; 
 

 
inputArray.forEach(function (a) { 
 
    this[a.subject] = this[a.subject] || { marks: [], noOfStudents: [] }; 
 
    this[a.subject].marks.push(a.marks); 
 
    this[a.subject].noOfStudents.push(a.noOfStudents); 
 
}, temp); 
 

 
outputArray = factorArray.map(function (a) { 
 
    var getSum = function (k) { 
 
     return this[a.subject] && this[a.subject][k].length ? '-(' + this[a.subject][k].join('+') + ')' : ''; 
 
    }.bind(this); 
 
    return { subject: a.subject, marks: a.marks + getSum('marks'), noOfStudents: a.noOfStudents + getSum('noOfStudents') }; 
 
}, temp); 
 

 
console.log(outputArray);

結果與計算項目

var inputArray = [{ subject: 'Maths', marks: '40', noOfStudents: '5' }, { subject: 'Science', marks: '50', noOfStudents: '16' }, { subject: 'History', marks: '35', noOfStudents: '23' }, { subject: 'Science', marks: '65', noOfStudents: '2' }, { subject: 'Maths', marks: '30', noOfStudents: '12' }, { subject: 'History', marks: '55', noOfStudents: '20' }], 
 
    factorArray = [{ subject: 'History', marks: '500', noOfStudents: '200' }, { subject: 'Maths', marks: '200', noOfStudents: '150' }, { subject: 'Science', marks: '300', noOfStudents: '100' }], 
 
    temp = Object.create(null), 
 
    outputArray = []; 
 

 
inputArray.forEach(function (a) { 
 
    this[a.subject] = this[a.subject] || { marks: 0, noOfStudents: 0 }; 
 
    this[a.subject].marks += +a.marks; 
 
    this[a.subject].noOfStudents += +a.noOfStudents; 
 
}, temp); 
 

 
outputArray = factorArray.map(function (a) { 
 
    var getSum = function (k) { 
 
     return (+a[k] - (this[a.subject] && this[a.subject][k] || 0)).toString(); 
 
    }.bind(this); 
 
    return { subject: a.subject, marks: getSum('marks'), noOfStudents: getSum('noOfStudents') }; 
 
}, temp); 
 

 
console.log(outputArray);

0

如果subjectfactorArray是獨一無二的:

var inputArray = [ 
 
    { subject: 'Maths', marks: '40', noOfStudents: '5' }, 
 
    { subject: 'Science', marks: '50', noOfStudents: '16' }, 
 
    { subject: 'History', marks: '35', noOfStudents: '23' }, 
 
    { subject: 'Science', marks: '65', noOfStudents: '2' }, 
 
    { subject: 'Maths', marks: '30', noOfStudents: '12' }, 
 
    { subject: 'History', marks: '55', noOfStudents: '20' } 
 
]; 
 

 
var factorArray = [ 
 
    { subject: 'History', marks: '500', noOfStudents: '200' }, 
 
    { subject: 'Maths', marks: '200', noOfStudents: '150' }, 
 
    { subject: 'Science', marks: '300', noOfStudents: '100' }  
 
]; 
 

 
// the result 
 
var outputArray = []; 
 

 
// loop through the factors 
 
for(var i = 0, numFactors = factorArray.length; i < numFactors; ++i) { 
 
    // this iteration 
 
    var thisFactor = factorArray[i]; 
 
    
 
    // make a copy 
 
    var thisOutput = { 
 
    "subject" : thisFactor.subject, 
 
    "marks" : thisFactor.marks, 
 
    "noOfStudents" : thisFactor.noOfStudents 
 
    }; 
 
    
 
    // loop through the input 
 
    for(var j = 0; j < inputArray.length; ++j) { 
 
    // this iteration 
 
    var thisInput = inputArray[j]; 
 
    
 
    // if the subject matches 
 
    if(thisInput.subject == thisOutput.subject) { 
 
     // do the calculation 
 
     thisOutput.marks -= thisInput.marks; 
 
     thisOutput.noOfStudents -= thisInput.noOfStudents; 
 
     
 
     // we don't need to loop through this one again so remove it from the inputArray 
 
     // and decrease j so we check it again on the "next" iteration 
 
     inputArray.splice(j--, 1); 
 
    } 
 
    } 
 
    
 
    // save it 
 
    outputArray.push(thisOutput); 
 
} 
 

 
console.log(outputArray);

0

我會通過級聯喜歡做減少了像

var inar = [{ subject: 'Maths', marks: '40', noOfStudents: '5' },{ subject: 'Science', marks: '50', noOfStudents: '16' },{ subject: 'History', marks: '35', noOfStudents: '23' },{ subject: 'Science', marks: '65', noOfStudents: '2' },{ subject: 'Maths', marks: '30', noOfStudents: '12' },{ subject: 'History', marks: '55', noOfStudents: '20' }], 
 
    facar = [{ subject: 'History', marks: '500', noOfStudents: '200' },{ subject: 'Maths', marks: '200', noOfStudents: '150' },{ subject: 'Science', marks: '300', noOfStudents: '100' }]; 
 

 
    outar = inar.reduce((p,c) => {var f = p.find(o => o.subject === c.subject); 
 
           f ? (f.marks = f.marks*1 + c.marks*1, 
 
             f.noOfStudents = f.noOfStudents*1 + c.noOfStudents*1) 
 
            : p.push(c); 
 
    \t       return p},[]) 
 
       .reduce((p,c) => {var f = p.find(o => o.subject === c.subject); 
 
           f.marks = f.marks*1 - c.marks*1; 
 
           f.noOfStudents = f.noOfStudents*1 - c.noOfStudents*1; 
 
           return p},facar); 
 
console.log(outar);