摘要:循環(huán)為數(shù)組每個(gè)項(xiàng),為索引循環(huán)遍歷每一項(xiàng),如果找到符合條件項(xiàng),后面便不再遍歷了。如果沒有則返回為初始值,為當(dāng)前項(xiàng)。
1. for循環(huán)
const arr = [0, 1, 2, 3]; for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }2. forEach
const arr = [0, 1, 2, 3]; arr.forEach((item, index) => console.log(item, index));
item為數(shù)組每個(gè)項(xiàng), index為索引
3. someconst arr = [0, 1, 2, 3]; arr.some((item, index) => item === 2);
some: 循環(huán)遍歷每一項(xiàng), 如果找到符合條件項(xiàng),后面便不再遍歷了。
4. everyconst arr = [0, 1, 2, 3]; arr.every((item, index) => item > 2);
every: 循環(huán)遍歷每一項(xiàng),如果找到不符合條件的項(xiàng),后面便不再遍歷了。
5. mapconst arr = [0, 1, 2, 3]; const mapArr = arr.map((item, index) => ({i: item}));
map: 循環(huán)遍歷每一項(xiàng),所返回的值是新數(shù)組的新項(xiàng)數(shù)值,原數(shù)組不會(huì)改變
6. filterconst arr = [0, 1, 2, 3]; const mapArr = arr.filter((item, index) => item < 2);
filter: 以數(shù)組形式篩選出符合條件的項(xiàng),如沒有符合的,返回空數(shù)組 []
7. for ofconst arr = [0, 1, 2, 3]; for (let v of arr) { console.log(v); }8. find
const arr = [0, 1, 2, 3]; arr.find(item => item > 2);
find: 循環(huán)遍歷每一項(xiàng),如果找到符合條件的項(xiàng), 便返回這個(gè)項(xiàng), 后面便不再遍歷了。如果沒有則返回undefined
9. findIndexconst arr = [0, 1, 2, 3]; arr.findIndex(item => item > 2);
findIndex: 循環(huán)遍歷每一項(xiàng),如果找到符合條件的項(xiàng),便返回這個(gè)項(xiàng)的索引, 后面便不再遍歷了。如果沒有則返回 -1
10. reduceconst arr = [0, 1, 2, 3]; const result = arr.reduce((total, item) => total + item);
total為初始值, item為當(dāng)前項(xiàng)。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/97779.html