ES5 方法
- forEach ,与 for 循环类似
- map ,返回一个新的数组
let arr = [ {title: "aaaa",read: 100,hot:true}, {title: "bbbb",read: 100,hot: false}, {title: "cccc",read: 100,hot: true}, {title: "dddd",read: 100,hot: true} ]; let newArr = arr.map(function(item, index, arr){ let json = {}; json.t = `hhhh${item.title}`; json.r = `rrrrrr${item.read+100}`; return json; })
- filter ,过滤,返回一个符合筛选条件的数组
let arr = [ {title: "aaaa",read: 100,hot:true}, {title: "bbbb",read: 100,hot: false}, {title: "cccc",read: 100,hot: true}, {title: "dddd",read: 100,hot: true} ]; let newArr = arr.filter((item, index, arr) => { return item.hot == true; })
- some ,数组中有一个元素符合条件就返回 true
let arr = ['apple', 'banana', 'orange']; let newArr = arr.some((val, index, arr) => { return val == 'app'; })
every ,数组中每个元素都符合条件才返回 true,否则返回 false
let arr = [1,3,5,7,9];
let newArr = arr.every((val, index, arr) => {
return val%2 == 1;
});
ES6 新增方法
-
Array.from()
let aLi = document.querySelectorAll("ul li");//NodeList //let newArr = [...aLi]; let newArr = Array.from(aLi);
-
Array.of() ,把一组值转化为数组
-
find() ,查找,找到返回第一个符合条件的数组成员,如果没有找到,返回undefined
-
findIndex(),没找到返回 -1
-
fill() ,arr.fill(填充的东西,开始位置,结束位置)
-
includes(),判断数组是否包含指定元素