Let's imagine I have this json
var personas = [
{name: "paco", edad:23},
{name: "pepe", edad:25},
{name: "lucas", edad:30}
]
But really I have 1000 people and I want to get all the names of the people and that they are not repeated, I can do a for
to go through all but I don't know if there is any other faster way to not go through 1000 objects
An easy and efficient way is to first sort the array, and remove the unique elements with just a for
The last element is going to compare with
undefined
, which doesn't matter because they are going to be differentThis algorithm is
O(N * logN)
for sorting andO(N)
removing duplicates, so it's pretty fast.This could help you...
You could make a custom filter to return what you need. Or even make one
angular.forEach
and store them in an array by contrasting them with aindexOf()
.