从列表中获取所有对象

时间:2017-02-25 16:29:34

标签: javascript arrays arraylist

所以,我有这样的事情:

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。

谢谢!

5 个答案:

答案 0 :(得分: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; }

答案 1 :(得分: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));




答案 2 :(得分: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
}

答案 3 :(得分: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);

答案 4 :(得分: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);