I have an array of about 15,000 javascript objects. Each object has two fields:
{
name : "Foo",
address : "[email protected]"
}
I want to create a new array which only stores unique email addresses and corresponding names. So far I have this method:
// temp1 is my array of 15,000 objects
var arr = [];
for (var i = 0; i<temp1.length; i++){
var count = 0;
if(!arr.length){arr.push(temp1[i])};
for(var x = 0; x<arr.length; x++){
if(temp1[i].address === arr[x].address){
count++;
if(temp1[i].name.length && !arr[x].name.length){arr[x] = temp1[i];} // Choose the new object if the old one has no name field
}
if((x === arr.length -1) && count === 0){
arr.push(temp1[i])
}
}
}
I have an added requirement in here - if the object in arr has a blank string as its name field, and the temp1 object does, I want to store the temp1 object instead.
My current method takes a good 30s to run in Chrome and this is not ideal.
EDIT: To clarify, I'm asking whether there is a more efficient method in Javascript to find unique objects in an array. One method above is to create a new array, iterate against the original and for each one loop through everything in the new array to check for duplicates. I'm wondering what's out that that will be more efficient than this.