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?
Add a comment
|
1 Answer
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.
2 Comments
C. Kearns
thanks jonasnas! _.uniq returns the duplicate free array, not the duplicates themselves, can i use it to switch the booleans of the actual duplicates?
C. Kearns
Brain wasn't functioning properly on Sunday morning, they start as false, so I just needed to flip the boolean of the returned array to true with a loop. Thanks for help, marking as solved!