var points = [{ x: 75, y: 25},{ x: 75+0.0046, y: 25}];
我该如何迭代这个东西。 我想先打印x和y的值,然后是第二个值等等....
var points = [{ x: 75, y: 25},{ x: 75+0.0046, y: 25}];
我该如何迭代这个东西。 我想先打印x和y的值,然后是第二个值等等....
使用Array#forEach
方法来迭代数组。
var points = [{
x: 75,
y: 25
}, {
x: 75 + 0.0046,
y: 25
}];
points.forEach(function(obj) {
console.log(obj.x, obj.y);
})
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) )
for..of
循环。
var points = [{ x: 75, y: 25},{ x: 75+0.0046, y: 25}];
for (let point of points) {
console.log(point.x, point.y);
}