0

I am currently working on a small home project, and I am trying to push an array into an array of arrays, if said array does not already exist in the array of arrays.

var arrayArr = [[1,4]];

function pushArr() {

    var tempArr = [1, 3];
    var tempArr2 = [1, 4];

    for(i = 0; i < arrayArr.length, i++)
        if(!arrayArr.indexOf(tempArr[i])) {
            arrayArr.push(tempArr[i]);
        } else {
            //other logic
        }

  } 

Now, I know this example does not really make sense in the real world, it's just to illustrate my concern. How do I search through an array of arrays to make sure, that I don't create duplicates.

If you have any questions, please ask.

Thanks !

MC Mowgli
  • 113
  • 7

4 Answers4

2

In my solution, in isArrayInArray(), I'm looping through each element in the main array arrayArr. I'm then comparing if the first and second element of each given array match. If so, the array has been added already so it'll return true.

var arrayArr = [[1, 4]];

pushArray([1, 4]); // does not get added
pushArray([1, 3]); // gets added

console.log(arrayArr);

function isArrayInArray(arrayToSearch, arrayToFind) {
    for (let i = 0; i < arrayToSearch.length; i++) {
        if (arrayToSearch[i][0] === arrayToFind[0] && arrayToSearch[i][1] === arrayToFind[1]) {
            return true;
        }
    }

    return false;
}

function pushArray(array) {
    if (!isArrayInArray(arrayArr, array)) {
        arrayArr.push(array);
    }
}
Toby Mellor
  • 8,093
  • 8
  • 34
  • 58
  • 1
    This only tests if the first two elements of each array are matched, correct? (i.e., it won't work for arrays of length 3 or more) – apsillers Apr 26 '17 at 19:11
  • @apsillers Correct! It was done under the assumption that the arrays were only of length 2. I can modify the function is OP requires :) – Toby Mellor Apr 26 '17 at 19:12
  • 1
    Yup, I just wanted to make sure the OP went into this with eyes open; it could indeed be exactly what they need. `:)` – apsillers Apr 26 '17 at 19:13
  • Exactly what I needed, my array only contains 2 elements each time, so this fits perfectly. Thank you very much! – MC Mowgli Apr 26 '17 at 19:19
0

You know [1,2] === [1,2] is false.

Please refer to How to Compare two Arrays are Equal using Javascript?

Community
  • 1
  • 1
Ovais
  • 374
  • 2
  • 7
0

Even if you have the following:

let a = [1,2];
let b = [1,2];

Both a and b hold two different references to to arrays that have the same numbers. They are not equal !

let a = [1,2];
let b = [1,2];
console.log(a === b);

If we by pass this and we assume that for our problem that two arrays with the same length and the same data are the same, we can try to call the function arrayCanBePushed, before we want to append an array to our array of arrays and if it returns true, then we can proceed with the push.

var arrayOfArrays = [[1,2],[2,3],[3,4,5],[7]];

function arrayCanBePushed(arr){
    return !arrayOfArrays.filter(a => a.length === arr.length)
                         .some(a => a.every(ele=>arr.includes(ele)));
}

console.log(arrayCanBePushed([1,2]));
console.log(arrayCanBePushed([1,2,3]));
Christos
  • 53,228
  • 8
  • 76
  • 108
0

Tried to keep the answer as simple as possible. Basically we are iterating over arrayArr and checking if all of the elements of the input array match all of the elements of any of the element arrays. So it should be simply an every nested in a some. However if we are comparing just two-element arrays (array to string conversion should be pretty fast) as you have now clarified in your answer then I would use string comparison as illustrated in pushArr1. This is the general direction you were heading in your first attempt so I added it to my answer.

var arrayArr = [[1, 3, 5]];
var tempArr = [1, 3, 5];
var tempArr2 = [1, 4];
pushArr1(tempArr);

function pushArr(temp)
{
    if(!arrayArr.some(function(e){
        return e.every(function(e1, i)
        {
          return e1 === temp[i];
        })
      })) arrayArr.push(temp);
      
      console.log(arrayArr);
}


function pushArr1(temp)
{
    if(!arrayArr.some(function(e){return e.join(",") === temp.join(",")})) arrayArr.push(temp);

      console.log(arrayArr);
}
Luke Kot-Zaniewski
  • 1,161
  • 11
  • 12