1

I have a couple of arrays which have some same ids. What I want to achieve is get the intersections between the arrays in plain javascript, no libraries. If there is a match of Ids and arraypicklist values are not equal between 2 or more arrays I should get an array with the matching Ids.

Below is my example which I tried but this ends up with no ids where I expect at least 1 match. In this case Id:123 as in the first and second array there is a match. So I would expect

intersection = [{"Id":"123","arrayPicklist":"Categorie__c"},{"Id":"123","arrayPicklist":"Regio__c"}];

fiddle:https://jsfiddle.net/ozckc0tw/4/

var buckets = [[{"Id":"123","arrayPicklist":"Categorie__c"}],
[{"Id":"123","arrayPicklist":"Regio__c"}],
[{"Id":"124","arrayPicklist":"Categorie__c"}],              
[{"Id":"123","arrayPicklist":"Regio__c"},{"Id":"125","arrayPicklist":"Regio__c"},{"Id":"123","arrayPicklist":
"Regio__c"},{"Id":"126","arrayPicklist":"Regio__c"}]]     

                            function IntersectionByKey(key) {
                    var i,
                        j,
                        k,
                        ret = [],
                        item,
                        args = [].slice.call(arguments, 1);

                    args.sort(function(a, b) {return a.length - b.length});                                      
                    i:for(i=0; i<args[0].length; i++) {
                        item = [Object.assign(args[0][i], {})];
                        j:for(j=1; j<args.length; j++) {
                            for(k=0; k<args[j].length; k++) {
                                if(key in args[0][i] && args[0][i][key] == args[j][k][key]){
                                    item.push(Object.assign(args[j][k], {}));
                                    continue j;
                                }
                            }
                            continue i; 
                        }
                        ret.push(item);
                    }
                    return ret;
                }
                var key = 'Id';
                var intersection = IntersectionByKey.apply(null, [key].concat(buckets));


                 console.log('intersection '+JSON.stringify(intersection))
Thomas
  • 181
  • 1
  • 9

1 Answers1

0

Done,Sorry, but I did not looked out your code, I tried my logic with your requirements, You can use it. my logic

var buckets = [
    [{
      "Id": "123",
      "arrayPicklist": "Categorie__c"
    }],
    [{
      "Id": "123",
      "arrayPicklist": "Regio__c"
    }],
    [{
      "Id": "124",
      "arrayPicklist": "Categorie__c"
    }],
    [{
      "Id": "123",
      "arrayPicklist": "Regio__c"
    }, {
      "Id": "125",
      "arrayPicklist": "Regio__c"
    }, {
      "Id": "123",
      "arrayPicklist": "Regio__c"
    }, {
      "Id": "126",
      "arrayPicklist": "Regio__c"
    }]
  ],
  intersection = [{
    "Id": "123",
    "arrayPicklist": "Categorie__c"
  }, {
    "Id": "123",
    "arrayPicklist": "Regio__c"
  }];
var resArray = [];

for (var j = 0; j < intersection.length; j++) {
  for (var i = 0; i < buckets.length; i++) {
    for (var k = 0; k < buckets[i].length; k++) {
      if (buckets[i][k].Id == intersection[j].Id && buckets[i][k].arrayPicklist == intersection[j].arrayPicklist) {
        resArray.push(buckets[i][k]);

      }
    }
  }

}
console.log(resArray);
nisar
  • 1,055
  • 2
  • 11
  • 26