How can i convert this array
var array = ['a', 'b', 'c', 'd', 'a', 'b','c', 'd']
to an Object
object = { a:2 , b:2, c:2, d:2 }
using only javascript underscore _reduce ?
How can i convert this array
var array = ['a', 'b', 'c', 'd', 'a', 'b','c', 'd']
to an Object
object = { a:2 , b:2, c:2, d:2 }
using only javascript underscore _reduce ?
arr.reduce((obj, currentKey) => {
if(currentKey in obj) {
obj[currentKey] = obk[currentKey] +1
} else{
obj[currentKey] = 1
}
return obj;
}, {});
and testd on your code, here is the output
I hope this will help
var array = ['a', 'b', 'c', 'd', 'a', 'b','c', 'd'];
var obj = [...new Set(array)]
.reduce((acc, cur) => ({
...acc,
[cur]: array.filter(item => item === cur).length
}),
{});
console.log(obj);