If i have an array of objects
[
{name: 'John', count: 5, isunique: false, occupation: 'carpenter'},
{name: 'Bob', count: 6, isunique: false, occupation: 'carpenter'},
{name: "John", count: 9, isunique: false, occupation: "barber"}
]
(this array is large), and I want to determine which occupations are unique, and on that condition return a new array where only the boolean isunique is changed like so:
[
{name: 'John', count: 5, isunique: false, occupation: 'carpenter'},
{name: 'Bob', count: 6, isunique: false, occupation: 'carpenter'},
{name: "John", count: 9, isunique: true, occupation: "barber"}
]
is this possible to do efficiently in Javascript with a large array? I am completely at a loss here.
One option is:
var count = data.reduce(function(ret, el) {
ret[el.occupation] = (ret[el.occupation] || 0) + 1;
return ret;
}, {});
data.forEach(function(el) { el.isunique = count[el.occupation] === 1; });