如何在Javascript中使用forEach方法?[已关闭]

mo49yndu  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(97)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question
我应该在函数中写什么?array.forEach(function(item) {})

nhaq1z21

nhaq1z211#

forEach()方法为数组中的每个元素调用一个函数。forEach()方法不为空元素执行。

let text = "";
const fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);

document.getElementById("demo").innerHTML = text;

function myFunction(item, index) {
  text += index + ": " + item + "<br>";
}
<!DOCTYPE html>
<html>

<body>

  <h1>JavaScript Arrays</h1>
  <h2>The forEach() Method</h2>

  <p>forEach() calls a function for each element in an array:</p>

  <p id="demo"></p>

</body>

</html>

相关问题