例如:
来自
[x0,x1,x2,x3] - x0 compares itself to x1,x2 and x3.
x1 compares itself to x0, x2 and x3. And so on...
至
[x0,x1,x2,x3] - x0 compares itself to x1,x2 and x3.
x1 compares itself to x2 and x3 only.
x2 only compares itself to x3.
x3 does not need to do any comparison at all.
本质上,我只想以一种方式遍历数组,忽略当前元素后面的每个元素。
for (var i = 0; i < boids.length; i++) {
//boids is an array containing elements "boid"
var d = distSquared(this.position.x, this.position.y, boids[i].position.x, boids[i].position.y);
//get distance between current boid and all other boids in array.
//Looking to change this to get distance between current boid and all other boids infront of this element in the array.
if (boids[i] == this) { //if the boid being compared is to its own self, skip it.
continue;
}
}
我将如何实现这样的结构?
最佳答案
您正在寻找的是这样的:
var a = [1,2,3,4];
for(var i=0; i<a.length; i++){
for(var j=i+1; j<a.length; j++){
console.log(i,j)
}
}
关于Javascript仅在当前对象之后迭代数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39673532/