使用javascript在数组中查找一组对象

时间:2017-02-08 14:47:38

标签: javascript arrays

问题已移至CodeReview: https://codereview.stackexchange.com/questions/154804/find-a-list-of-objects-in-an-array-with-javascript

拥有一个对象数组 - 例如数字 - 如果找到一组子对象,最佳(内存和CPU效率)方式是什么?举个例子:

demoArray = [1,2,3,4,5,6,7]
查找[3,4,5]将返回2,而查找60将返回-1
该函数必须允许换行,因此查找[6,7,1,2]将返回5

我有一个当前的工作解决方案,但我想知道它是否可以以任何方式进行优化。



var arr = [
  1,
  5,2,6,8,2,
  3,4,3,10,9,
  1,5,7,10,3,
  5,6,2,3,8,
  9,1]
var idx = -1
var group = []
var groupSize = 0

function findIndexOfGroup(g){
  group = g
  groupSize = g.length
  var beginIndex = -2
  
  while(beginIndex === -2){
    beginIndex = get()
  }
  
  return beginIndex
}

function get(){
    idx = arr.indexOf(group[0], idx+1);
    
    if(idx === -1 || groupSize === 1){
      return idx;
    }
    var prevIdx = idx
    
    for(var i = 1; i < groupSize; i++){
      idx++
      
      if(arr[getIdx(idx)] !== group[i]){
        idx = prevIdx
        break
      }
      
      if(i === groupSize - 1){
        return idx - groupSize + 1
      }
    }
    return -2
}

function getIdx(idx){
  if(idx >= arr.length){
    return idx - arr.length
  }
  return idx
}

console.log(findIndexOfGroup([4,3,10])) // Normal
console.log(findIndexOfGroup([9,1,1,5])) // Wrapping
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:4)

您可以使用reminder operator %将索引保持在数组的范围内,并使用Array#every检查搜索数组的每个元素。

function find(search, array) {
    var index = array.indexOf(search[0]);

    while (index !== -1) {
        if (search.every(function (a, i) { return a === array[(index + i) % array.length]; })) {
            return index;
        }
        index = array.indexOf(search[0], index + 1);
    }
    return -1;
}

console.log(find([3, 4, 5], [1, 2, 3, 4, 5, 6, 7]));          //  2
console.log(find([6, 7, 1, 2], [1, 2, 3, 4, 5, 6, 7]));       //  5
console.log(find([60], [1, 2, 3, 4, 5, 6, 7]));               // -1
console.log(find([3, 4, 5], [1, 2, 3, 4, 6, 7, 3, 4, 5, 9])); //  6
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

我对这个问题的看法是使用slice()并将每个长度等于组长度的子阵列与实际的组数组进行比较。可能需要一点时间,但代码足够短:

&#13;
&#13;
// The array to run the tests on
var arr = [
  1,
  5, 2, 6, 8, 2,
  3, 4, 3, 10, 9,
  1, 5, 7, 10, 3,
  5, 6, 2, 3, 8,
  9, 1
];
// Check arrays for equality, provided that both arrays are of the same length
function arraysEqual(array1, array2) {
  for (var i = array1.length; i--;) {
    if (array1[i] !== array2[i])
      return false;
  }
  return true;
}
// Returns the first index of a subarray matching the given group of objects
function findIndexOfGroup(array, group) {
  // Get the length of both arrays
  var arrayLength = array.length;
  var groupLength = group.length;
  // Extend array to check for wrapping
  array = array.concat(array);
  var i = 0;
  // Loop, slice, test, return if found
  while (i < arrayLength) {
    if (arraysEqual(array.slice(i, i + groupLength), group))
      return i;
    i++;
  }
  // No index found
  return -1;
}
// Tests
console.log(findIndexOfGroup(arr,[4,3,10])); // Normal
console.log(findIndexOfGroup(arr,[9,1,1,5])); // Wrapping
console.log(findIndexOfGroup(arr,[9,2,1,5])); // Not found
&#13;
&#13;
&#13;

如果组长于数组,可能会出现一些错误,但我会将其留给您扩展方法来处理这种情况。