var r,
ary = ['apple', 'strawberry', 'banana', 'pear', 'apple', 'orange', 'orange', 'strawberry'];
r = ary.filter(function (currentValue, index, arr) {
return arr.indexOf(currentValue) === index;
});
console.log(r.toString());
/* 去除重復元素依靠的是indexOf總是返回第一個元素的位置,
后續的重復元素位置與indexOf返回的位置不相等,因此被filter濾掉了。*/
參數 | 描述 |
---|---|
currentValue | 必須。當前元素的值 |
index | 可選。當前元素的索引值 |
arr | 可選。當前元素屬於的數組對象 |