2017-02-25 56 views
1

所以,我有這樣的事情:從列表中獲取的所有對象

objArray1 = [ { candidate1: "Alex" , votes: 4}, { candidate2: "Paul", votes: 3}]; 

objArray2 = [ { candidate1: "Alex" , votes: 7}, { candidate2: "Ben", votes: 3}, { candidate3: "Melisa", votes:8 }]; 

我試圖使用JavaScript來製造陣列的所有候選人,看看有多少票他們每個人都有。計算投票的部分很容易,但我不知道如何將所有候選人放在一個數組中。

我應該得到一個數組:Alex,Paul,Ben和Melisa。

謝謝!

+2

你不應該給每個元素的 「候選」 若干底。相反,只是稱它爲候選人,如果你需要一個ID,有一個ID字段。然後你可以很容易地做objArray1.concat(objArray2).map(function(entry){return entry.candidate}); – JohnD

回答

0
var candidates = []; 
var found = 0; 
for(var i=0;objArray1.length>i;i++){ 
    found = 0; 
    //add votes to candidate array 
    for(var j=0;candidates.length>j;j++){ 
     if(candidates[j].name==objArray1[i][Object.keys(objArray1[i])[0]]){ 
      candidates[j].votes = candidates[j].votes+objArray1[i].votes; 
      found = 1; 
     } 
    } 
    //if condidate not found in votes array, create new 
    if(found==0){ 
     var tmp = {}; 
     tmp.name = objArray1[i].candidate; 
     tmp.votes = objArray1[i].votes; 
     //add to array 
     candidates.push(tmp); 
    } 
} 

console.log(candidates); 
2

您可以使用散列表和按名稱組。

var array1 = [ { candidate1: "Alex" , votes: 4}, { candidate2: "Paul", votes: 3}], 
 
    array2 = [ { candidate1: "Alex" , votes: 7}, { candidate2: "Ben", votes: 3}, { candidate3: "Melisa", votes:8 }], 
 
    grouped = [array1, array2].reduce(function (hash) { 
 
     return function (r, a) { 
 
      a.forEach(function (o, i) { 
 
       var name = o['candidate' + (i + 1)]; 
 
       if (!hash[name]) { 
 
        hash[name] = { candidate: name, votes: 0 }; 
 
        r.push(hash[name]); 
 
       } 
 
       hash[name].votes += o.votes; 
 
      }); 
 
      return r; 
 
     }; 
 
    }(Object.create(null)), []); 
 
    
 
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

生成持有屬性名作爲名稱和計票結果爲值的對象。

var objArray1 = [ { candidate1: "Alex" , votes: 4}, { candidate2: "Paul", votes: 3}], objArray2 = [ { candidate1: "Alex" , votes: 7}, { candidate2: "Ben", votes: 3}, { candidate3: "Melisa", votes:8 }]; 
 

 
var res = [] 
 
    // cobine two arrays 
 
    .concat(objArray1, objArray2) 
 
    // iterate over the arrays 
 
    .reduce(function(obj, o) { 
 
    // get the key except the votes 
 
    var key = Object.keys(o).find(function(k) { 
 
     return k != 'votes'; 
 
    }) 
 
    // define property if not already defined 
 
    obj[key] = obj[key] || 0; 
 
    // add the vote count 
 
    obj[key] += o.votes; 
 
    // return object refernece 
 
    return obj; 
 
    // set initial value as empty object 
 
    }, {}); 
 

 
console.log(res); 
 
// get the names array if need 
 
console.log(Object.keys(res));

0

要得到名稱的列表,你問

var rawArrays = objArray1.concat(objArray2), Candidates = [], tmp = [] 
for (var i in rawArrays) { 
    tmp[rawArrays[i][Object.keys(rawArrays[i])[0]]] = 1 
} 
Candidates = Object.keys(tmp) 

要獲得

var rawArrays = objArray1.concat(objArray2), Candidates = [] 

for (var i in rawArrays) { 
    name = rawArrays[i][Object.keys(rawArrays[i])[0]] 
    if (Candidates[name]) Candidates[name] += rawArrays[i].votes 
    else Candidates[name] = rawArrays[i].votes 
} 
0

簡短的解決方案使用Array.prototype.concat()與候選人和投票求和陣列,Array.prototype.reduce()Array.prototype.map()功能:

var objArray1 = [ { candidate1: "Alex" , votes: 4}, { candidate2: "Paul", votes: 3}], 
 
    objArray2 = [ { candidate1: "Alex" , votes: 7}, { candidate2: "Ben", votes: 3}, { candidate3: "Melisa", votes:8 }], 
 
    
 
    grouped = objArray1.concat(objArray2).reduce(function(r, o){ 
 
     var k = Object.keys(o).filter(function(k){ 
 
       return k.indexOf('candidate') === 0; 
 
      })[0]; 
 

 
     (r[o[k]])? r[o[k]].votes += o.votes : r[o[k]] = {candidate: o[k], votes: o.votes}; 
 
     return r; 
 
    }, {}), 
 

 
    result = Object.keys(grouped).map(function(k){ return grouped[k]; }); 
 
    
 
console.log(result);