2

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.

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

4
  • To be clear, are you looking to get the number of times a candidate appears, or total up the number of votes for that candidate? Commented Jun 22, 2016 at 15:39
  • 1
    canidate !== candidate. What is your question? Commented Jun 22, 2016 at 15:40
  • You have a type entry.canidate should be candidate. Also, entry.candidate is not a collection. entry is a collection. Commented Jun 22, 2016 at 15:41
  • Sorry. Yes, I'm looking to get the total times that each candidate appears. Commented Jun 22, 2016 at 15:44

7 Answers 7

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Why didn't use reduce like other answers?
because in this case reduce returns always the object count, which is not necessary.
1

You can create a new object with the summation of votes by candidates like this:

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

Comments

0

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

Comments

0

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

Comments

0

You can easily do this with 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);

Comments

0

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.

Comments

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

2 Comments

you may consider adding some explanation over your answer.
i am so sorry that to answer without any explanation, and it is my first time answer question in stackoverflow..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.