Say I have array like this:
[
\"foo\",
\"bar\",
\"foo\"
\"bar\",
\"bar\",
\"bar\",
\"zoom\"
]
I want to group it so I
Explanation: First, we create object (a brand new empty object). Secondly using the spread operator we first copy everything from the existing array and make a new array out of it and use that to create a new Set (the Set object lets you store unique
values) and use that as the key, and finally we filter out our array (with a little help from the length property) to see how many times a particular key
occurs and set that to the object's value
. And remember, since we used Object.assign()
and put everything inside of it, so what gets returned is an object containing the result of everything that I tried to explain (to the best of my knowledge) above :) We also used the comma operator here which simply evaluates each of its operands (from left to right) and returns the value of the last operand.
const arr = ["foo", "bar", "foo", "bar", "bar", "bar", "zoom"];
const data = Object.assign({}, ...Array.from(new Set(arr), key => ({[key]: arr.filter(value => value === key).length})));
console.log(data);
Yes you can reduce()
your array to an object with the keys and the count, like this:
const input = [
"foo",
"bar",
"foo",
"bar",
"bar",
"bar",
"zoom"
];
const result = input.reduce((total, value) => {
total[value] = (total[value] || 0) + 1;
return total;
}, {});
console.log(result);
Hope it helps!
Just use the function Array.prototype.reduce
.
let array = [ "foo", "bar", "foo", "bar", "bar", "bar", "zoom"],
result = array.reduce((a, c) => (a[c] = (a[c] || 0) + 1, a), Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could use the reduce()
method which is avalible on the array object to achieve this grouping. So, something along the lines of this might achieve what you're after:
var input = [
"foo",
"bar",
"foo",
"bar",
"bar",
"bar",
"zoom"
]
// Iterate the input list and construct a grouping via a "reducer"
var output = input.reduce(function(grouping, item) {
// If the current list item does not yet exist in grouping, set
// it's initial count to 1
if( grouping[item] === undefined ) {
grouping[item] = 1;
}
// If current list item does exist in grouping, increment the
// count
else {
grouping[item] ++;
}
return grouping;
}, {})
console.log(output)
You can do this in a concise way via reduce:
var arr = [ "foo", "bar", "foo", "bar", "bar", "bar", "zoom" ]
var result = arr.reduce((r,c) => (r[c] = (r[c] || 0) + 1, r), {})
console.log(result)
It gets really cute if you ware to use lodash and _.countBy:
var arr = [ "foo", "bar", "foo", "bar", "bar", "bar", "zoom" ]
var result = _.countBy(arr);
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Yeah I guess with Array.prototype.reduce, it's just:
const map = list.reduce((a, b) => {
a[b] = a[b] || 0;
return ++a[b], a;
}, {});
wondering if there is less verbose way tho.