JavaScript函数,接受一个多维数组和一个单一数组,查找单一数组在多维数组中的匹配项。

4

我在Stack Overflow上查找了一个类似的问题,但它并没有完全涵盖我所要做的内容。我一直在尝试让它工作,但是始终得到错误的结果。现在我已经无计可施了。以下是我的问题示例:

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]

展示(添加到您的问题中)您迄今为止的最佳努力,并解释您卡在哪里以及为什么。 - PM 77-1
再检查一下输出吧!我觉得你忘了一个! - ibrahim mahrir
3
为什么 [1,9,11] 不在最终结果中? - RomanPerekhrest
你有5个子数组要返回4个输出.. 怎么回事.. - Redu
对不起,我漏打了那个,但是没错。[1,9,11] 也将是其中一个输出。 - Jovanny
谢谢你的回答。这是我第一次使用堆栈,并不断努力提高我的编程技能。我感谢每个人的意见!我的代码有问题,我用了三个循环。一个是循环遍历我的单个数组,另外两个是用来循环遍历我的多维数组。然后我尝试使用if语句来比较两个循环数组的索引,并将匹配项推送到新数组中**if (x[a] == y[b][c]) { res.push(x[a]) }; 但这给我带来了不想要的结果。下次我有问题时,我会确保包括我已经完成的代码。 - Jovanny
5个回答

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));


1
使用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));


0
您可以使用这个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)可以在几乎恒定的时间内完成。


0
你可以将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; }


0
根据您的逻辑,您在问题中得出了错误的输出:
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

试一下,这应该可以工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接