I have an array of objects with known keys. And I want to count how many objects have a particular key with a certain value. Currently I made it up with this:
counter = () => {
if (Array) {
let a = 0;
for (let i = 0; i < Array.length; i++) {
if (Array[i].key === '1' || Array[i].key === '2' || Array[i].key === '3' || Array[i].key === '4' || Array[i].key === '5' || Array[i].key === '6' || Array[i].key === '7') {
a++;
}
}
return a;
}
}
I tried reduce() and find() while converting arrays to objects and vice versa but with no luck yet.
I suppose there should be more elegant way to perform such a simple task in plain vanilla JS ES6 or ES7. Ideally a oneliner. No lodashing please.
All other questions here target searching in elements, when mine touch one level deeper.