Reputation: 1671
I have 3 arrays of objects parsed out of large CSV files. They have the same format: [{ count: 2, name: "foo", unique: false}, {count: 4, name: "foo", unique: false}]
and each array contains exactly 500 objects. I am looking for an elegant way to check the value of the name key against all three arrays and if it only exists in one, change the boolean value of the unique key to true. I can't seem to think of anything but brute force approaches which are proving expensive. Does not have to be vanilla javascript, underscore, etc are fine. Any ideas?
Upvotes: 0
Views: 1094
Reputation: 3580
You can concatenate 3 Arrays and then search for uniques with underscore (you said you don't mind):
var all = a1.concat(a2).concat(a3)
_.uniq(all, function(obj){
return obj.count;
});
Also _.uniq(array, [isSorted], [iteratee])
gives you an option to sort array to improve performance, but it depends on the use case of the program if it is worth it.
In general I wouldn't worry too much about performance until you know its an issue.
Upvotes: 2