https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
語法:
循環對數組中的元素調用callback函數, 如果返回true 保留,如果返回false 過濾掉, 返回新數組,老數組不變
var new_array = source_array.filter(callback(element,index, array))
備注:
a. 類似與 array.map
b. 原來的數組不變
eg:
過濾掉數組中的重合的元素
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);