循环通过Javascript对象返回值的总和

时间:2016-06-22 15:38:28

标签: javascript jquery arrays loops object

我要做的是循环一个对象,获取每个候选者出现的总时间并将其打印到页面。

我一直在讨厌这个问题。我是javascript的新手,使用对象对我来说是一件非常新鲜事。

感谢您的帮助!

var entry = [
  {
    candidate : "guy1",
    rank : 1,
    state : "AK",
    vote_count : "2"
  },
  {
    candidate : "guy2",
    rank : 1,
    state : "MI",
    vote_count : "3"
  },
  {
    candidate : "guy3",
    rank : 1,
    state : "AK",
    vote_count : "5"
  },
  {
    candidate : "guy2",
    rank : 1,
    state : "AL",
    vote_count : "4"
  },
  {
    candidate : "guy2",
    rank : 1,
    state : "FL",
    vote_count : "9"
  },
  {
    candidate : "guy1",
    rank : 1,
    state : "MN",
    vote_count : "7"
  }
];

for ( var i = 0, l = entry.canidate.length; i < l; i++ ) {
    guy1 += entry.canidate[i];
}

console.log(guy1);

7 个答案:

答案 0 :(得分:3)

您可以使用候选人作为关键字并计算投票数。

&#13;
&#13;
git prune
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用以下候选人的投票总数创建一个新对象:

https://jsfiddle.net/he58xsb8/

var entry = [
  {
    candidate : "guy1",
    rank : 1,
    state : "AK",
    vote_count : "2"
  },
  {
    candidate : "guy2",
    rank : 1,
    state : "MI",
    vote_count : "3"
  },
  {
    candidate : "guy3",
    rank : 1,
    state : "AK",
    vote_count : "5"
  },
  {
    candidate : "guy2",
    rank : 1,
    state : "AL",
    vote_count : "4"
  },
  {
    candidate : "guy2",
    rank : 1,
    state : "FL",
    vote_count : "9"
  },
  {
    candidate : "guy1",
    rank : 1,
    state : "MN",
    vote_count : "7"
  }
];

var voteByCandidate = {};
for ( var i = 0; i < entry.length; i++ ) {
    var result = entry[i];
    if (!voteByCandidate[result.candidate]) {
        voteByCandidate[result.candidate] = 0;
    }
    voteByCandidate[result.candidate] += result.vote_count * 1; // Convert to number.
}

console.log(voteByCandidate);

答案 2 :(得分:0)

您可以创建候选映射和循环条目数组。对于每个相同的候选者,递增映射中的键。

var candidateMap = {};

entry.forEach( function(candidate) {
   candidateMap[candidate.candidate] = candidateMap[candidate.candidate] || 0;
candidateMap[candidate.candidate] += 1;
}); 

答案 3 :(得分:0)

您可以使用reduce:

轻松完成此操作

var entries = [{ candidate: "guy1", rank: 1, state: "AK", vote_count: "2" }, { candidate: "guy2", rank: 1,  state: "MI", vote_count: "3" }, { candidate: "guy3", rank: 1, state: "AK", vote_count: "5" }, { candidate: "guy2", rank: 1, state: "AL", vote_count: "4" }, { candidate: "guy2", rank: 1, state: "FL", vote_count: "9" }, { candidate: "guy1", rank: 1, state: "MN", vote_count: "7" }];
var result = entries.reduce(function(r, c) {
  r[c.candidate] = (r[c.candidate] || 0) + parseInt(c.vote_count);
  return r;
}, {});
console.log(result);

答案 4 :(得分:0)

  

我希望得到每个候选人出现的总时间

您可以使用数组filter过滤候选人,然后reduce来计算每个候选人的出现次数

var _filter=[];
entry.filter(function(item){
  _filter.push(item.candidate)
})

var _reducedArray =_filter.reduce(function(prev,next){
        prev[next] = (prev[next] + 1) || 1;
        return prev;
    },{});

console.log(_reducedArray);

JSFIDDLE

答案 5 :(得分:0)

这可能就是您所需要的:

const votes = {};
entry.foraEach(item => {
    if(!votes.hasOwnProperty(item.candidate)) {
        votes[item.candidate] = +item.vote_count;
    } else votes[item.candidate] += +item.vote_count;
});

您将拥有votes对象内的所有候选人和投票。

答案 6 :(得分:-1)

var entry = [
  {
    candidate : "guy1",
    rank : 1,
    state : "AK",
    vote_count : "2"
  },
  {
    candidate : "guy2",
    rank : 1,
    state : "MI",
    vote_count : "3"
  },
  {
    candidate : "guy3",
    rank : 1,
    state : "AK",
    vote_count : "5"
  },
  {
    candidate : "guy2",
    rank : 1,
    state : "AL",
    vote_count : "4"
  },
  {
    candidate : "guy2",
    rank : 1,
    state : "FL",
    vote_count : "9"
  },
  {
    candidate : "guy1",
    rank : 1,
    state : "MN",
    vote_count : "7"
  }
];

function getCandidateTimes(entry, candidateName){
  var appearTimes = 0;
  for (var i=0, len = entry.length; i < len; i++){
    if(entry[i].candidate == candidateName){
      appearTimes++;
    }
  }
  return appearTimes;
}

var guy1AppearTimes = getCandidateTimes(entry, 'guy1');
console.log('guy1 appears ' + guy1AppearTimes + ' times!');

var guy2AppearTimes = getCandidateTimes(entry, 'guy2');
console.log('guy2 appears ' + guy2AppearTimes + ' times!');

//and so on.......