迭代後,在數組對象例如:的Javascript目前唯一的對象
從
[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;
}
}
我該如何去實現這樣的結構?
歡迎來到SO!只是一個指針,我只看到1 for循環。你能分享比較是如何完成的嗎? – Rajesh
所以即使'this'也會出現在'boids'數組中。所以只需在數組中搜索'this'的索引並相應地初始化'i'。 – Rajesh