第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

js數(shù)組遍歷-Looping over arrays

標(biāo)簽:
JavaScript

jstips Looping over arrays

JS语言中有很多遍历数组的方法。我们从几个较为传统的遍历方式开始,逐步向大家展示标准的遍历方法。

while
let index = 0;
const array = [1, 2, 3, 4, ,5];

while (index < array.length) {
    console.log(array[index]);
    index++;
}
for(最传统的形式)
const array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i ++) {
    console.log(array[i]);
}
forEach
const array = [1, 2, 3, 4, 5];

array.forEach((current_value, index, arr) => {
    console.log(current_value, index);
});
map

forEach这中方法,并不返回一个新的数组。map函数对数组的每个元素上应用一个函数,并返回一个全新的数组。map是immutable的,更加的安全。

//.map(current_value, index, array)
const array = [1, 2, 3, 4, 5];
const square = x => Math.pow(x, 2);
const squares = array.map(square);
console.log(`Original array: ${array}`);
console.log(`Squared array: ${squares}`);
reduce

reduce函数对数组的每个元素执行累加器,最后返回一个值。

const array = [1, 2, 3, 4, 5];
const sum = (x, y) => x + y;
const array_sum = array.reduce(sum, 0);
console.log(`The sum of array: ${array} is ${array_sum}`);
filter

对一个数组实施过滤

const array = [1, 2, 3, 4, 5];
const event = x => x % 2 === 0;
const event_arr = array.filter(event);
console.log(event_arr);
every

判断一个数组上的元素是否全部符合某种条件

const array = [1, 2, 3, 4, 5, 6];
const under_seven = x => x < 7;

if (array.every(under_seven)) {
  console.log('Every element in the array is less than 7');
} else {
  console.log('At least one element in the array was bigger than 7');
}
some

判断一个数组上的元素是否至少有一个符合某种条件

const array = [1,2,3,9,5,6,4];
const over_seven = x => x > 7;

if (array.some(over_seven)) {
  console.log('At least one element bigger than 7 was found');
} else {
  console.log('No element bigger than 7 was found');
}

from: xixi小站

點(diǎn)擊查看更多內(nèi)容
2人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消