1.map
有返回值,返回一個新的數(shù)組,每個元素為調(diào)用func的結(jié)果。
let list = [1, 2, 3, 4, 5];let other = list.map((d, i) => { return d * 2;});console.log(other);// print: [2, 4, 6, 8, 10]
2.filter
有返回值,返回一個符合func條件的元素數(shù)組
let list = [1, 2, 3, 4, 5];let other = list.filter((d, i) => { return d % 2;});console.log(other);// print: [1, 3, 5]
3.some
返回一個boolean,判斷是否有元素符合func條件,如果有一個元素符合func條件,則循環(huán)會終止。
let list = [1, 2, 3, 4, 5];list.some((d, i) => { console.log(d, i); return d > 3;});// print: 1,0 2,1 3,2 4,3// return false
4.every
返回一個boolean,判斷每個元素是否符合func條件,有一個元素不滿足func條件,則循環(huán)終止,返回false。
let list = [1, 2, 3, 4, 5];list.every((d, i) => { console.log(d, i); return d < 3;});// print: 1,0 2,1 3,2// return false
5.forEach
沒有返回值,只針對每個元素調(diào)用func。
優(yōu)點:代碼簡介。
缺點:無法使用break,return等終止循環(huán)。
let list = [1, 2, 3, 4, 5];let other = [];list.forEach((d, i) => { other.push(d * 2);});console.log(other);// print: [2, 4, 6, 8, 10]
6.for in
for-in循環(huán)實際是為循環(huán)”enumerable“對象而設(shè)計的,for in也可以循環(huán)數(shù)組,但是不推薦這樣使用,for 主站蜘蛛池模板: 区。| 六盘水市| 卢龙县| 陈巴尔虎旗| 广水市| 阳春市| 绥滨县| 卓尼县| 婺源县| 花莲县| 凤城市| 鹤壁市| 孝昌县| 宜阳县| 德州市| 舞阳县| 崇州市| 宁远县| 雅安市| 乌拉特后旗| 古交市| 米林县| 高青县| 靖州| 光泽县| 贵德县| 石首市| 巴林左旗| 鲁甸县| 平陆县| 双峰县| 冷水江市| 东方市| 施甸县| 海晏县| 会同县| 类乌齐县| 莱州市| 额敏县| 阳西县| 河间市|