代碼示例:
數(shù)組變量.forEach(值變量名 => {代碼塊})
//每遍歷一個值,就作為形參傳入給代碼塊,執(zhí)行一次該函數(shù)頭,繼續(xù)遍歷
? 舉例說明:
<script>
let arr = [1, 2, 3, 4];
//arr.forEach(val => {});
arr.forEach(val => {
console.log(val);
});
</script>
brr.forEach(val => {
console.log(val + ",你好");
})
crr.forEach(o => {
console.log(o.name);
})
drr.forEach(o => {
if(o.age < 18) console.log(o.age);
})
文章來源:http://www.zghlxwxcb.cn/news/detail-827476.html
!注意:只遍歷有值的元素文章來源地址http://www.zghlxwxcb.cn/news/detail-827476.html
到了這里,關于JavaScript——forEach()方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!