【问题标题】:JavaScript function that takes a multidimensional array and a single array, and finds matches of the single array in the multi-d array接受多维数组和单个数组的 JavaScript 函数,并在多维数组中查找单个数组的匹配项
【发布时间】:2017-02-11 19:41:47
【问题描述】:

我尝试在堆栈溢出中查看类似的问题,但它并没有完全满足我想要做的事情,我一直试图让它工作,但不断得到错误的结果。在这一点上,我简直不知所措。我的意思的例子

var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]]
var compareArray = [9,11,13,15,1,2,5,6]

function intersect(a, b) {
//code to compare both arrays and find a match
}

console.log(Intersect(compareArray, masterArray)) 

输出将是

[9,11,13,15]
[1,2,5,6]
[5,13]
[13,15]

【问题讨论】:

  • 展示(添加到您的问题中)您迄今为止所做的最大努力,并解释您遇到困难的地方和原因。
  • 再次检查输出!我想你忘了一个!
  • 为什么[1,9,11]不在最终结果中?
  • 你有 5 个子数组来返回 4 个输出.. 怎么样..
  • 对不起,我错过了输入那个,但是是的。 [1,9,11] 也是输出之一。

标签: javascript arrays multidimensional-array


【解决方案1】:

使用Array.prototype.reduce 获取所有交叉点的数组,如下所示:

var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]];
var compareArray = [9,11,13,15,1,2,5,6];


function intersect(multi, simple) {
  return multi.reduce(function(res, b) {
    var intersection = simple.reduce(function(r, e) { // get the intersection of the current array and compareArray (simple)
      if(b.indexOf(e) != -1) // if the current element of the current array is also in the compareArray then push it into the intersection array
        r.push(e);
      return r;
    }, []);
    
    res.push(intersection); // push the intersection array into the result array
    return res;
  }, []);
}

console.log(intersect(masterArray, compareArray));

【讨论】:

    【解决方案2】:

    使用具有特定正则表达式模式的RegExp对象的解决方案(将compareArray数字转换为正则表达式交替组项)。
    使用的附加功能: String.prototype.match(), Array.prototype.join(), Array.prototype.map()

    var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]],
        compareArray = [9,11,13,15,1,2,5,6];
    
    function intersect(compareArray, masterArray) {
        var pattern = new RegExp('\\b(' + compareArray.join('|') + ')\\b', 'g'),
            result = [];
    
        masterArray.forEach(function(v) {
            var matches = v.join(' ').match(pattern);
            if (matches.length) result.push(matches.map(Number));
        });
    
        return result;
    }
    
    console.log(intersect(compareArray, masterArray));

    【讨论】:

      【解决方案3】:

      根据您的逻辑,您的问题输出错误:

      var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]]
      var compareArray = [9,11,13,15,1,2,5,6]
      
      
      function intersect(a, b) {
        var temp = [];
         k = 0;
         for(i = 0; i < b.length; i++){
            temp1 = []
            for(j=0;j<b[i].length;j++){
               if($.inArray(b[i][j], a) > -1){
                  temp1.push(b[i][j]);
      
               }
            }
            temp.push(temp1);
         }
         return temp;
      }
      
      console.log(intersect(compareArray, masterArray));
      

      检查这个jsfiddle

      试一试,应该可以的。

      【讨论】:

        【解决方案4】:

        你可以使用这个 ES6 函数:

        function intersect(a, b) {
            a = new Set(a); // for faster lookup
            return b.map(c => c.filter(a.has.bind(a))).filter(Boolean);
        }
        // Demo
        const masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]];
        const compareArray = [9,11,13,15,1,2,5,6];
        console.log(intersect(compareArray, masterArray).join('\n'));

        .filter(Boolean) 部分是可选的,只需要排除根本没有匹配数字的数组,否则在结果中将由空数组表示。

        使用Set 将提高性能,因为在Set(使用has)中的查找可以在几乎恒定的时间内完成。

        【讨论】:

          【解决方案5】:

          您可以映射masterArray 的过滤结果。

          var masterArray = [[1, 2, 5, 6], [5, 13, 7, 8], [9, 11, 13, 15], [13, 14, 15, 16], [1, 9, 11, 12]],
              compareArray = [9, 11, 13, 15, 1, 2, 5, 6],
              result = masterArray.map(function (a) {
                  return a.filter(function (b) {
                      return compareArray.indexOf(b) !== -1;
                  });
              });
          
          console.log(result);
          .as-console-wrapper { max-height: 100% !important; top: 0; }

          ES6

          var masterArray = [[1, 2, 5, 6], [5, 13, 7, 8], [9, 11, 13, 15], [13, 14, 15, 16], [1, 9, 11, 12]],
              compareArray = [9, 11, 13, 15, 1, 2, 5, 6],
              result = masterArray.map(a => a.filter(b => compareArray.includes(b)));
          
          console.log(result);
          .as-console-wrapper { max-height: 100% !important; top: 0; }

          【讨论】:

            猜你喜欢
            • 2021-03-30
            • 2017-10-11
            • 2016-05-26
            • 1970-01-01
            • 1970-01-01
            • 2018-07-13
            • 1970-01-01
            • 2015-12-24
            • 2015-10-05
            相关资源
            最近更新 更多