亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

NodeJS 基礎(chǔ)教程

NodeJS Express.js

NodeJS 緩沖&URL

NodeJS MySql

NodeJS MongoDB

NodeJS 文件(FS)

NodeJS 其他

Node.js forEach

Node.js forEach用于為每個(gè)元素執(zhí)行提供的功能。

語(yǔ)法– forEach

forEach的語(yǔ)法為;

let arr = [element1, element2, elementN]; 
arr.forEach(myFunction(element, index, array, this){ function body });

myFunction函數(shù)對(duì)每個(gè)執(zhí)行element在arr。element在每次迭代期間,數(shù)組的of作為參數(shù)傳遞給函數(shù)。

示例1:元素?cái)?shù)組上的forEach

在此示例中,我們將使用forEach應(yīng)用于數(shù)組的每個(gè)元素。

let array1 = ['a1', 'b1', 'c1']; 
array1.forEach(function(element) { 
  console.log(element); 
 });

輸出結(jié)果

a1
b1
c1

示例2:forEach元素?cái)?shù)組上具有作為參數(shù)傳遞的外部函數(shù)

在此示例中,我們將使用forEach應(yīng)用于數(shù)組的每個(gè)元素。然后,我們分別定義函數(shù)并將其作為參數(shù)傳遞給forEach。

let array1 = ['a1', 'b1', 'c1'] 
let myFunc = function(element) { 
  console.log(element) 
 } 
array1.forEach(myFunc)

示例3:可以訪問元素,索引和數(shù)組的數(shù)組上的forEach

在此示例中,我們將在每次迭代中訪問索引和數(shù)組以及元素。

let array1 = ['a1', 'b1', 'c1'] 
 
let myFunc = function(element, index, array) { 
  console.log(index + ' : ' + element + ' - ' + array[index]) 
 } 
 
array1.forEach(myFunc)

輸出結(jié)果

0 : a1 - a1
1 : b1 - b1
2 : c1 - c1