简体   繁体   English

遍历Javascript对象返回值的总和

[英]looping through Javascript Object returning sum of values

What I'm trying to do is loop through an object getting the total times that each candidate appears and print that to the page. 我想做的是遍历一个对象,获取每个候选人出现的总时间并将其打印到页面上。

I've have been racking my head on this one. 我一直在努力奋斗。 I'm new to javascript and working with objects is something very new to me. 我是javascript新手,使用对象对我来说是很新的东西。

Thank you for the help! 感谢您的帮助!

 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); 

You could use an object with the candidates as key and count the votes. 您可以将候选对象用作关键对象并计算票数。

 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" }], count = {}; entry.forEach(function (a) { count[a.candidate] = (count[a.candidate] || 0) + +a.vote_count; }); console.log(count); 

You can create a new object with the summation of votes by candidates like this: 您可以使用候选人的总和创建一个新对象,如下所示:

https://jsfiddle.net/he58xsb8/ 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);

You can create a candidateMap and loop entry array .for each same candidate increment the key in the map. 您可以创建一个候选人地图和循环条目数组。对于每个相同的候选人,递增地图中的键。

var candidateMap = {};

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

You can easily do this with reduce: 您可以使用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); 

I'm looking to get the total times that each candidate appears 我正在寻找每个候选人出现的总时间

You can use array filter to filter the candidates then reduce to count the number of occurrence of each candidate 您可以使用数组过滤器过滤候选者,然后减少以计算每个候选者的出现次数

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 的jsfiddle

This might be what you need: 这可能是您需要的:

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

You will have all the candidates and votes inside the votes object. 您将在votes对象中拥有所有候选人和投票。

 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....... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM