0

I have a function that pushes values into an array (very simplified example below):

numOfArr = 3; //is dynamically retrieved. can be 1-500);

arr1 = [212,214,215,218]
arr2 = [212,259,214,292,218]
arr3 = [272,214,218,292]

I run a for loop on each of the "arr" arrays to push their values into "myArr" (which works fine) and use this to get the values that are in "arr1" AND in "arr2" through "arr500", etc. I tried to do was to iterate through myArr, create a temporary copy of the array, and using .indexOf to check for other values before removing them, but I know I'm making a mistake somewhere that I can't find..

for(var i = 0; i < myArr.length; i++){
    tempArr = myArr;
     for(var j = 0; j < numOfArr; j++){
        var spl = tempArr.indexOf(myArr[i]);
        if(spl == -1 && myArr.indexOf(myArr[i]) !== -1){
            myArr.splice(i,1);
        } else {
            tempArr.splice(i,1);
        }
     }
}

Ideally, what I want is "myArr" to only end up with the values that are in all 3 arr arrays (214 and 218). Is the issue in where I remove splice on myArr because the loop is still going through everything?

Update:

Using the example here: Simplest code for array intersection in javascript

function intersect_safe(a, b)
{
  var ai=0, bi=0;
  var result = new Array();

  while( ai < a.length && bi < b.length )
  {
     if      (a[ai] < b[bi] ){ ai++; }
     else if (a[ai] > b[bi] ){ bi++; }
     else /* they're equal */
     {
       result.push(a[ai]);
       ai++;
       bi++;
     }
  }

  return result;
}

I tried to implement it in my code as I need.

var allItems = [];
var items = [1,2,3,4,5,6,7]
allItems.push(items);
allItems.push([1,2,4,9,100,1000])
allItems.push([1,2,3,4,5,9,100])

for(var i=0; i < allItems.length; i++){
    //iterate through item

    console.log("items "+ items);
    console.log("allItems id: "+allItems[i]);
    items = intersect_safe(items,allItems[i]);
    console.log("items: "+items);
}

If I use the function on the actual values that are in each index of the array, I see a proper intersection is returned so that I only have "1,2,4" as values in the final "items" array. In this loop, the function seems to return the original value every time. Apologies for missing anything blatant... I just can't find a break in the logic if each index in the array contains an array itself that I can dig deeper into.

Community
  • 1
  • 1
  • `concat` returns a new array, so `myArr` is not getting the new items if that's what you meant to do. – elclanrs Mar 10 '15 at 22:24
  • ah okay.. well what I actually did was use .push in a for loop on each of the arrays. I just tried to make it simple in this example. Sorry, I will change it. –  Mar 10 '15 at 22:25

1 Answers1

1

Mathematically you're looking for the intersection of two sets.

A quick stack overflow check yielded:

Simplest code for array intersection in javascript

update here's a simple example of how you could use your intersect_safe method against three arrays.

var array1 = [3,4,5];
var array2 = [2,3,4];
var array3 = [1,2,3];

function intersect_safe(a, b)
{
  var ai=0, bi=0;
  var result = new Array();

  while( ai < a.length && bi < b.length )
  {
     if      (a[ai] < b[bi] ){ ai++; }
     else if (a[ai] > b[bi] ){ bi++; }
     else /* they're equal */
     {
       result.push(a[ai]);
       ai++;
       bi++;
     }
  }

  return result;
}

var intersectArray = intersect_safe(array1,array2);
intersectArray = intersect_safe(intersectArray, array3);
console.log(intersectArray); //logs 3

Hope this example helps you extend the function for recursion :)

Community
  • 1
  • 1
MarcJames
  • 189
  • 9
  • 1
    Welp. Was going to post that! Just a reminder that the intersection of many sets is the same as intersecting the first two and then recursively intersect the result with the others in any order. – Sergio Mar 10 '15 at 22:30
  • I'm still having some sort of issue.. The function works until I try to call it within my array. I'm not sure if there are issues with it or if I'm just delusional at 2am.. I've updated my question. –  Mar 11 '15 at 05:29
  • Thank you, @Sergio. That does make sense to me, so I have tried to use this by constantly checking against the same array in my example. –  Mar 11 '15 at 05:45
  • Thanks for the update! My issue was that I was checking against an array that had an array as its first index instead of just pushing the individual values in.. It all came down to one stinky variable :( –  Mar 11 '15 at 14:52