简体   繁体   English

JavaScript中数组的交集

[英]intersection of arrays in javascript

I have array something like this: 我有这样的数组:

var array1 = [
          {"name":"a","groups":["xxx","yyy"]},
          {"name":"abc","groups":["xxx","yyy"]},
          {"name":"abcd","groups":["zzz","xxx","yyy"]}
        ];

and

var array2 = ["xxx","yyy"];

I need to return the entire index of array1 when both "xxx" and "yyy" of array2 matches only to the "xxx" and "yyy" of array1. 当array2的“ xxx”和“ yyy”都只匹配array1的“ xxx”和“ yyy”时,我需要返回array1的整个索引。

Like in this example, it should only return array1[0] and array1[1]. 像本例一样,它仅应返回array1 [0]和array1 [1]。 Any help will be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

Modern JS would be: 现代JS将是:

 function filter(array1, array2) { // to filter array1 based on array2 return array1.filter(function(elt) { // retain an elt in array1 var groups = elt.groups; // if its groups property return groups.length === array2.length && // has the same length as array2 groups.every(function(e) { // and every element in it return array2.indexOf(e) > -1; // is found in array2 }); }); } var array1 = [ {"name":"a","groups":["xxx","yyy"]}, {"name":"abc","groups":["xxx","yyy"]}, {"name":"abcd","groups":["zzz","xxx","yyy"]} ]; var array2 = ["xxx","yyy"]; document.writeln(JSON.stringify(filter(array1, array2))); 

var array1 = [
          {"name":"a","groups":["xxx","yyy"]},
          {"name":"abc","groups":["xxx","yyy"]},
          {"name":"abcd","groups":["zzz","xxx","yyy"]}
        ];


var array2 = ["xxx","yyy"];

function checkArrays( arrA, arrB ){
    //check if lengths are different
    if(arrA.length !== arrB.length) return false;
    //slice so we do not effect the orginal
    //sort makes sure they are in order
    var cA = arrA.slice().sort(); 
    var cB = arrB.slice().sort();
    for(var i=0;i<cA.length;i++){
         if(cA[i]!==cB[i]) return false;
    }
    return true;
}



for (var i in array1){
    if (checkArrays(array1[i].groups, array2)){
        alert(array1[i]);
    }
}

http://jsfiddle.net/hh1tyy5e/ http://jsfiddle.net/hh1tyy5e/

JavaScript does not have built in list comparison method. JavaScript没有内置的列表比较方法。
Anyways, above is working code that will alert the desired elements from array1 无论如何,上面是可以提醒array1所需元素的工作代码

I used stringify for comparison so the code doesn't change for a generic "something inside an object property" (ie the content of "groups" - or whatever) - performance could be better if you use direct compares. 我使用stringify进行比较,因此对于通用的“对象属性内的内容”(即“组”的内容-或其他内容),代码不会更改-如果使用直接比较,性能可能会更好。

var array1 = [
  {"name":"a","groups":["xxx","yyy"]},
  {"name":"abc","groups":["xxx","yyy"]},
  {"name":"abcd","groups":["zzz","xxx","yyy"]}
];

var testgroup = ["xxx", "yyy"];
var tg = JSON.stringify(testgroup);

var filteredArray = array1.filter(function (el) {
    if (JSON.stringify(el.groups) == tg) return el;
});
alert(JSON.stringify(filteredArray));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM