0

Lets say i have an array like this:

let votesArr = [yes,no,yes,no,yes];

and i want to to count how much times every word repeats itself and push to another so the output looks like this:

let votesData = [3,2]; // 3 for three yeses and 2 for two nos.

and i want to to work on many types of arrays like this, lets say an array that has 3 or 4 unique word. I'm trying for a lot of time already and can't do that.

  • please add for other words some use cases with result and what you have tried. – Nina Scholz Dec 18 '17 at 16:38
  • That data structure is kind of poor... you have no indication of what the numbers in the second array are referring to... You should push objects instead: `let votesData = [{value: 'yes', count: 3},{value: 'no', count: 2}]`. Then loop while your array has a size, get the first instance, filter out all instances that match that, remove them from the array you are looping, and add the size to your votesData. Something along those lines? – Tyler Dahle Dec 18 '17 at 16:43
  • @TylerDahle The data structure is like that because i'm using ChartJS and you can put the data only in arrays and not i arrays of objects. –  Dec 18 '17 at 16:49

5 Answers5

0

Just returning a plain array of counts will not make sense I guess. It should be more like below. If you don't want this output then just map the values to form an array.

{
  "yes": 3,
  "no": 2
}

let votesArr = ["yes","no","yes","no","yes"];

const mappedArr = votesArr.reduce((a, b) => { 
  a[b] = a[b] || 0;
  a[b] += 1;
  return a;
}, {});

console.log(mappedArr);
Nandu Kalidindi
  • 6,075
  • 1
  • 23
  • 36
0

You can do this as follows:

let votesArr = ["yes","no","yes","no","yes"];

let countSummary = votesArr.reduce( (count, val) => {
    if(!count[val]) { count[val] = 0 }
        count[val]++;
    return count;
}, {})
console.log(countSummary)// {yes: 3, no: 2}
let countSummmaryArr = Object.keys(countSummary).map(k=>countSummary[k]);

console.log(countSummmaryArr )// [3,2]

The way this works is that the .reduce counts every instance to a map of values, and the .map converts it to an array of the values.

asosnovsky
  • 2,158
  • 3
  • 24
  • 41
0

The below does what you need, although I'm sure it could be cleaned up a bit.

var data = ["Unsure", "Yes", "Yes", "No", "Yes", "No", "Maybe", "Unsure"];
var counts = {};
for (var i = 0; i < data.length; i++) {

    (counts[data[i]]) ? counts[data[i]]++ : counts[data[i]] = 1; 

}

// counts = {Unsure: 2, Yes: 3, No: 2, Maybe: 1}
0

You could use the power of Map.

var array = ['yes', 'no', 'yes', 'no', 'yes'],
    map = new Map,
    result;

array.forEach(v => map.set(v, (map.get(v) || 0) + 1));
result = [...map.values()];

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can do like this

let votesArr = ['yes', 'no', 'yes', 'no', 'yes'];
// Create an empty object to store array item as key & its 
// number of repeat as value
var itemObj = {};
// loop over it and store the value in the object
var m = votesArr.forEach(function(item) {
  if (!itemObj[item]) {
    itemObj[item] = 1
  } else {
    itemObj[item] = itemObj[item] + 1
  }
});
// Use object.values to retrive the value
console.log(Object.values(itemObj))
brk
  • 48,835
  • 10
  • 56
  • 78