使用javascript在数组中查找一组对象(Find a group of objects in an array with javascript)

编程入门 行业动态 更新时间:2024-10-26 21:18:57
使用javascript在数组中查找一组对象(Find a group of objects in an array with javascript)

问题已移至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 
  
 

Question has been moved to CodeReview: https://codereview.stackexchange.com/questions/154804/find-a-list-of-objects-in-an-array-with-javascript

Having an array of objects - such as numbers - what would be the most optimal (Memory and CPU efficiency) way if finding a sub group of objects? As an example:

demoArray = [1,2,3,4,5,6,7] Finding [3,4,5] would return 2, while looking for 60 would return -1. The function must allow for wrapping, so finding [6,7,1,2] would return 5

I have a current working solution, but I'd like to know if it could be optimized in any way.

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 
  
 

最满意答案

您可以使用提醒操作符%将索引保持在数组范围内,并使用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; } 
  
 

You could use the reminder operator % for keeping the index in the range of the array with a check for each element of the search array with 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; } 
  
 

更多推荐

本文发布于:2023-07-26 05:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271344.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   对象   javascript   Find   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!