As with JavaScript 1.6, array.filter () can be used to remove duplicate values:
[1, 4, 7, 1, 2, 1, 3, 1, 4] .filter(function (value, index, self) { return self.indexOf(value) === index; });
returns
[1, 4, 7, 2, 3]
What's good about the filter is that it works with all types of values ββin an array:
[1, "d", 4, "e", 7, "e", 1, "a", 2, "d", 1, "b", 3, "c", 1, "e", 4] .filter(function (value, index, self) { return self.indexOf(value) === index; });
returns
[1, 2, 3, 4, 7, "a", "b", "c", "d", "e"]
Erik anderson
source share