How to iterate over arrays and objects in JavaScript
本问题已经有最佳答案,请猛点这里访问。
1 | var points = [{ x: 75, y: 25},{ x: 75+0.0046, y: 25}]; |
我将如何迭代这个过程。我想先打印x和y值,然后再打印2nd和soo。
数组迭代使用
1 2 3 4 5 6 7 8 9 10 11 | var points = [{ x: 75, y: 25 }, { x: 75 + 0.0046, y: 25 }]; points.forEach(function(obj) { console.log(obj.x, obj.y); }) |
您可以使用
1 2 3 4 | var points = [{ x: 75, y: 25},{ x: 75+0.0046, y: 25}]; for (let point of points) { console.log(point.x, point.y); } |
1 2 3 4 5 6 7 8 | var points = [{ x: 75, y: 25},{ x: 75+0.0046, y: 25}]; //ES5 points.forEach(function(point){ console.log("x:" + point.x +" y:" + point.y) }) //ES6 points.forEach(point=> console.log("x:" + point.x +" y:" + point.y) ) |