Javascript仅在当前对象之后迭代数组中的对象

标签 javascript arrays

例如:

来自

[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/

相关文章:

php - CI_model 未正确捕获 POSTed JSON 数据

java - 带循环的 boolean 值用于检查数组

arrays - 快速复制具有相同地址的对象

c++ - 返回 char* 形式的 C++ 函数

javascript - HighCharts:摆脱工具提示但保持十字准线

javascript - polymer CSS 问题

javascript - JS打印pdf文件

javascript - 使用 JavaScript 时,如何创建自动单击链接并在新选项卡中打开它的代码?

Java 通过 CSV 进行过滤并查找数据中的空白

java - 用 JPanel 数组制作棋盘