filter():建立指定函式,由原陣列過濾指定函數產生新陣列

filter():建立指定函式,由原陣列過濾指定函數產生新陣列

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => 
    word.length > 6
);

console.log(result);
//Array ["exuberant", "destruction", "present"]

Filter去掉重複的元素

1
2
3
4
5
6
7
8
9
let arr = ['B', 'A', 'E', 'C', 'A', 'F', 'G', 'E'];

let newArr = arr.filter( (element, index, arry)=> {
//
console.log('arry',arry)
return arry.indexOf(element) === index;
});

console.log(newArr)