利用filter過濾去重


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 可選。當前元素屬於的數組對象
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM