js array.filter實例(數組去重)

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filterjavascript

語法:html

循環對數組中的元素調用callback函數, 若是返回true 保留,若是返回false 過濾掉,  返回新數組,老數組不變java

var new_array = source_array.filter(callback(element,index, array))數組

 

備註:函數

 a. 相似與 array.mappost

 b. 原來的數組不變 .net

eg:prototype

過濾掉數組中的重合的元素 htm

var source_arr = ['a', 'b', 'a', 'c', 'a', 'd', '1',1,'1'];
var array_unique = source_arr.filter(function (element, index, array) {
    return array.indexOf(element) === index;
});
console.log(array_unique);
console.log(source_arr);

  


 

 

相關文章
相關標籤/搜索