-2

I want delete array from array of arrays without iterate on them.

the first array is like this :

array1 = [ 
[
[{"id":"A1","y":12},{"id":"A4","y":12}],
[{"id":"A2","y":1}], 
[{"id":"A3","y":6}]  
] 

the second array is :

array2 = [{"id":"A1","y":12},{"id":"A4","y":12}]

I use javascript and i want delete array2 from array1.

So sorry for wrong description by the way i found that in my exemple the second array array is

array2 = [{ "id": "A1", "y": 12 }, { "id": "A4", "y": 2 }] 

So the two arrays have just one columns which is equal. How i can delete array1 from array2 even they have just one attribute which is equal ?

Thank you.

3 Answers3

2

splice is usually used to remove a particular item from the array. I think you want to do something like this:

    var array = [

        [{"name": "sik", "gender": "male"}],
        [{"name": "sug", "gender": "female"}],
        [{"name": "hyd", "gender": "male"}]

    ];

    // it removes the second array from the array: removes the female one.
    array.splice( 1, 1 );

Description: The function splice takes two arguments, first one is for the index of item, and second one until how much you want to delete.

For Example: splice( 2, 5 ) - means that: from the index 2, keep deleting until 5 items.

Hope this helps, Thanks.

Sikandar Sahab
  • 638
  • 3
  • 10
  • 27
0

Feels weird, but you can check every element of the array and match to the second array. Either way you need to iterate through your array. You can't simply type array1[i] == array2, you need to check it differently. A hack is to stringify them as JSON and compare strings, but there are other methods you can use to compare arrays.

Here is a simple demo with a for loop

array1 = [
  [{ "id": "A1", "y": 12 }, { "id": "A4", "y": 12 }],
  [{ "id": "A2", "y": 1 }],
  [{ "id": "A3", "y": 6 }]
]
array2 = [{ "id": "A1", "y": 12 }, { "id": "A4", "y": 12 }]
l = array1.length
for (var i = 0; i < l; i++) {
  // if arrays match
  if (JSON.stringify(array1[i]) == JSON.stringify(array2)) {
    // delete the element at `i` position
    array1.splice(i, 1);
    i--;
    l--;
    // break; // if you can guarantee that no more instances will occur 
  }
}
console.log(array1)
Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
  • i found that in my exemple the second array array is array2 = [{ "id": "A1", "y": 12 }, { "id": "A4", "y": 2 }] So the two arrays have just one columns which is equal. How i can delete array1 from array2 even they have just one attribute which is equal ? – Lembared Nabil Apr 18 '18 at 11:02
0

In Plain Javascript, we cannot filter them. This will work if u are dealing with valued datatypes. Here you are comparing arrays which are a referenced datatype. so here we need to write our own search stack

function searchForArray(haystack, needle){
var i, j, current;
for(i = 0; i < haystack.length; ++i){
if(needle.length === haystack[i].length){
  current = haystack[i];
  for(j = 0; j < needle.length && needle[j] === current[j]; ++j);
  if(j === needle.length)
    return i;
}
}
return -1;
}

var arr = [[1,3],[1,2]];
var n   = [1,3];

console.log(searchForArray(arr,n));

You can refer this question which has some reference resources too. Check whether an array exists in an array of arrays?

I know you asked for a solution without iteration, but i just thought to give an idea of references which may be useful for u.

CharanbabuKarnam
  • 336
  • 2
  • 5
  • 18