Javascript 数组方法查找超过第一个值的 indexOf

Javascript array method to find indexOf past the first value

我有一个数组,arr = [0, 0, 0, 0, 1, 1]。传递给函数的参数包含此数组以及在此示例中恰好为 1 的数值。我的函数被称为成对的,看起来像这样:

function pairwise(arr, arg) {
  ...some code ...
}
pairwise([0, 0, 0, 0, 1, 1], 1);

如果数组中的元素对加起来等于传递给函数的第二个参数,那么我需要添加每个唯一对的索引和 return 索引的总和。对于此示例,总和将为 10,因为索引 0 和 4 相加为 1,索引 1 和 5 相加为 1,因此索引 0 + 4 + 1 + 5 = 10 的总和。我不能对同一个索引进行两次计数。

在我的函数中,我在遍历数组长度的 for 循环中从 arr[i] 中减去 arg。然后我从该减法中获取结果并使用 arr.indexOf(result) 查找索引对(如果存在)。在我遇到这个问题之前,一切正常。 indexOf 只查找第一次出现,当我 运行 我的代码时,它不计算 arr 中的第二个 1,所以我无法获得第二对。我的总和是 4,而它应该是 10。这是我的其余代码:

function pairwise(arr, arg) {
  var array = [];
  if (arr.length != 0) {
    for (var i=0; i<arr.length; i++) {
      if (arr.indexOf(arg - arr[i]) != -1) {
        if (array.indexOf(i) === -1 && array.indexOf(arr.indexOf(arg-arr[i])) === -1) {
          if (i !== arr.indexOf(arg - arr[i])) {
            array.push(i,arr.indexOf(arg - arr[i]));
          }
        }  
      }
    }
  } else {
    return 0;
  }
  console.log (array);
  return array.reduce(function(a,b) {return a+b;});
}

pairwise([0, 0, 0, 0, 1, 1], 1); 

我还将结果推入数组,因此我仅限于 Array.prototype 方法。我尝试寻找其他方法来使用,但我找不到任何我可以用我正在做的事情来实现的方法。也许您知道一种更简单的方法来做到这一点?我的问题是,有没有办法查看 indexOf 的第一个索引匹配项。我应该使用其他方法来解决我的问题吗?

我的问题是,有没有办法查看 indexOf 的第一个索引匹配。

可以使用indexOf

的第二个参数fromIndex

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

来自索引:

The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the whole array will be searched. Default: 0 (entire array is searched).

示例:

var arr = [0, 0, 1, 2, 3];

alert(arr.indexOf(0, arr.indexOf(0) + 1)); // to get the index of second zero

要获取某个值的所有索引,您可以使用 Array.prototype.reduce 函数来 return 一个包含所有匹配索引的数组。

function indexAll(array, match){
    return array.reduce(function(inds,val,i){
        if(val == match) inds.push(i);
        return inds;
    },[]);
}
//  indexAll([0,1,2,3,2,3,2,1,0], 2)   will be [2, 4, 6]
//  indexAll([0,1,2,3,2,3,2,1,0], 4)   will be []
//  indexAll([0,1,2,3,2,3,2,1,0], 0)   will be [0, 8]

但是对于您正在寻找的用途,处理数组的副本然后删除已使用的项目可能会更好。

function pairwise(arr, arg) {
  var indexSum=0;
  var arrCopy = arr.slice(0);    // copy of array to modify
  var len=arr.length;            // cached for performance
  for (var i = 0; i < len ; i++) {
    if( arrCopy[i] !== undefined){  //only check if index has not been used 
  //  var j =arrCopy.indexOf(arg -arrCopy[i]);
  //  if(j > -1 && i != j){
  //    sumIndex += i + j;  // add the indexes
  //    delete arrCopy[i];  // delete indexes
  //    delete arrCopy[j];
  //  }  
      for(var j = i+1; j < len; j++){
        if (arrCopy[j] !== undefined) {
          if (arrCopy[i]+arrCopy[j] == arg){
            indexSum += i + j;  // add the indexes
            delete arrCopy[j];  // sets arrCopy[j] to undefined to stop reuse
            j = len;         //jump to next i value
          }
        }
      }
    }
  } 
  return indexSum
}

我没有使用 indexOf,而是使用了嵌套的 for 循环,因为 for 循环通常更快。 indexOf 实现被注释掉了。见 indexOf vs. for loop