0

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?

1 Answer 1

2

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.

http://underscorejs.org/#uniq

Sign up to request clarification or add additional context in comments.

2 Comments

thanks jonasnas! _.uniq returns the duplicate free array, not the duplicates themselves, can i use it to switch the booleans of the actual duplicates?
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.