如何使用JavaScript循环遍历数组中的所有条目?
如何使用JavaScript循环遍历数组中的所有条目?
forEach()
,它会像这样 -theArray.forEach ( element => {
console.log(element);
});
for()
,它会像这样 -for(let idx = 0; idx < theArray.length; idx++){
let element = theArray[idx];
console.log(element);
}
迭代器
来提高一些效率。迭代器是某些JavaScript集合(如Map
、Set
、String
、Array
)的属性。甚至,for..of
在底层使用迭代器
。next
方法,您可以访问当前项。下一个方法将返回当前项的值
和一个布尔值
,指示您是否已经到达了集合的末尾。以下是从数组创建迭代器的示例。values()
方法将常规数组转换为迭代器,如下所示:
const myArr = [2,3,4]
let it = myArr.values();
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());
Symbol.iterator
将常规数组转换为迭代器,如下所示:
const myArr = [2,3,4]
let it = myArr[Symbol.iterator]();
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());
数组
转换为迭代器
,方法如下:
let myArr = [8, 10, 12];
function makeIterator(array) {
var nextIndex = 0;
return {
next: function() {
return nextIndex < array.length ?
{value: array[nextIndex++], done: false} :
{done: true};
}
};
};
var it = makeIterator(myArr);
console.log(it.next().value); // {value: 8, done: false}
console.log(it.next().value); // {value: 10, done: false}
console.log(it.next().value); // {value: 12, done: false}
console.log(it.next().value); // {value: undefined, done: true}
注意:
iterable
。在这种情况下,请使用for..in
,因为它不使用值而使用键。您可以在此处阅读有关iteration protocol
的更多信息。
您可以使用:
ForEach
theArray.forEach(function (array, index) {
console.log(index);
console.log(array);
});
for
for(var i=0; i<theArray.length; i++) {
console.log(i)
}
map
theArray.map(x => console.log(x));
map
theArray.filter(x => console.log(x));
还有很多其他的迭代方式。
let ddl = new Array();
if (subjects) {
subjects.forEach(function (s) {ddl.push({"id": s.id, "label": s.name});});
}
我认为for/of
是正确的做法:
const arr = ['a', 'b', 'c'];
for (const v of arr) {
console.log(v); // Prints "a", "b", "c"
}
与 for/in
不同,for/of
在数组上跳过非数字属性。例如,如果你设置了 arr.foo = 'test'
,for (var v in arr)
将循环遍历 'foo'
键。
与 forEach()
不同,for/of
不会跳过数组中的“空洞”。const arr = ['a',, 'c']
是有效的 JavaScript 代码,只是第二个元素是一个“空洞”。该数组在功能上等同于 ['a', undefined, 'c']
。
你可以在这篇关于 for/of
和 forEach()
的博客文章中阅读更多信息。
map
:theArray.map(instance => do_something);
// Looping through arrays using the foreach ECMAScript 6 way
var data = new Array(1, 2, 3, 4, 5);
data.forEach((val,index) => {
console.log("index: ", index); // Index
console.log("value: ", val); // Value
});
var a = ["car", "bus", "truck"]
a.forEach(function(item, index) {
console.log("Index" + index);
console.log("Element" + item);
})
我喜欢这样做。
foreach
const arr = ["apple", "banana", "orange", "pear", "grape"];
arr.forEach(function(element) {
console.log(element);
});
for..of
const arr = ["apple", "banana", "orange", "pear", "grape"];
for (const element of arr) {
console.log(element);
}
/* Get all forms */
document.querySelectorAll( "form" ).forEach( form => {
/* For each form, add the onsubmit event */
form.addEventListener( "submit", event => {
event.preventDefault(); // Return false
/* Display it */
alert(event.target.action);
console.log(event.target);
} );
} );
<form action="form1.php" >
<input type="submit" value="Submit" />
</form>
<form action="form2.php" >
<input type="submit" value="Submit" />
</form>
<form action="form3.php" >
<input type="submit" value="Submit" />
</form>