NodeJS 基礎(chǔ)教程

NodeJS Express.js

NodeJS 緩沖&URL

NodeJS MySql

NodeJS MongoDB

NodeJS 文件(FS)

NodeJS 其他

Node.js forEach

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

語法– forEach

forEach的語法為;

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

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

示例1:元素數(shù)組上的forEach

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

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

輸出結(jié)果

a1
b1
c1

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

在此示例中,我們將使用forEach應(yīng)用于數(shù)組的每個元素。然后,我們分別定義函數(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
丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清