I have an array of arrays and need to filter them to have unique based off the first 2 items in the arrays.
[
["18", "-66"],
["26", "-80"],
["26", "-80"],
["26", "-80"],
["27", "-80"],
["27", "-80"],
["28", "-97"],
["29", "-99"],
["30", "-81"],
["30", "-87"],
["30", "-99"],
["31", "-110"],
["31", "-110"],
["31", "-111"],
["31", "-84"],
["31", "-87"],
["31", "-95"],
["31", "-95"],
["31", "-95"]
]
While a function such as this does a great job with a single unique, I fail to see how to modify it to search the first two items:
function filterArray(incomingArray){
var unique = {};
var distinct = [];
for( var i in incomingArray ){
if( typeof(unique[incomingArray[i][0]]) == "undefined"){
distinct.push([incomingArray[i][0],{}]);
}
unique[incomingArray[i][0]] = 0;
}
return distinct;
}
The entire array doesn't need to be unique, just the first two items, so for example the following would match:
[
["26", "-80", 2],
["26", "-80", 3]
]
## Update ##
I tried each suggested method and found some interesting things. First let me say thank you for each of your ideas / ways of solving the problem. In performance testing, I supplied 4000 records and did a performance test on each method.
Mark's method of using Set() with 4000 records finished in 19ms, when supplied only about 50 records it finished in 2ms.
D. Seah's method with with 4000 records finished in 149ms, but when supplied only 50 records finished in under 1ms.
vol7ron's method with 4000 records finished in 30ms, but oddly took 52ms with 50 records
Ben's method I am still working through how it works and so far haven't had it return what I expected, but with it returning a 3 dimensional array, I am interested to see what possibilities there are in other applications, but it took around 38 seconds and I ended up with a very large array of undefined. It is possibly something I am doing wrong, but at least in this setting, it may be more capable than I need, and since the other solutions are fast enough then I may table it till next time.
Using Set() looks like the best way to go for a performance dependent solution where the dataset may keep growing, but in a smaller dataset vol7tron wins.