for,in循环数组:
for (var i in arr) {
console.log("for in=>index=" + i + "/value=" + arr[i]);
}
for,of循环数组:
for (let value of arr) {
console.log("for of=>value=" + value);
}
for,in 循环遍历对象
var data = {1: "test1", 2: "test2"};
for(var i in data){
console.log(i);//索引
console.log(data[i]);//值
}
使用forEach遍历
results = arr.forEach(function (value, index, array) {
console.log("forEach=>" + index + ": " + value + "/" + array[index]);
return value;
});
console.log("forEach=>results=" + results); // =>undefined
使用map遍历
results = arr.map(function (value, index, array) {
console.log("map=>" + index + ": " + value + "/" + array[index]);
return value + index;
});
console.log("map=>
对象,数组循环遍历知识点
最新推荐文章于 2021-04-07 13:31:50 发布
本文详细探讨了JavaScript中对象和数组的遍历方法,包括for循环、forEach、map、filter等,还涉及了浅拷贝和深拷贝在遍历过程中的应用,以及如何避免遍历过程中遇到的问题和陷阱。通过对这些知识点的学习,读者将能够更高效地操作和遍历JavaScript的数据结构。
摘要由CSDN通过智能技术生成