[英]What to do if i want to show the first 5 elements from the array by using forEach loop?
There are 10 elements in an array.数组中有 10 个元素。 What to do if i want to show the first 5 elements from the array by using forEach loop?
如果我想使用 forEach 循环显示数组中的前 5 个元素怎么办?
let arrays =[1, 2, 4, 6, 7, 44, 5, 7, 6]
arrays.forEach(array =>{}) //
To show the first 5 elements from the array by using forEach loop, you can use if statement要使用 forEach 循环显示数组中的前 5 个元素,可以使用 if 语句
const arrays = [1, 2, 4, 6, 7, 44, 5, 7, 6];
arrays.forEach(function(value, index, array) {
if(index <= 4) {
document.write(value + "<br>");
}
});
Where parameters:其中参数:
https://www.w3schools.com/jsref/jsref_foreach.asp https://www.w3schools.com/js/js_if_else.asp https://www.w3schools.com/jsref/jsref_foreach.asp https://www.w3schools.com/js/js_if_else.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.