how to remove an array from a 2D (nested) array?

Viewed 123

I have a 2D array of certain coordinates :

var array = [ [5, 34],  [31, 82], [10, 31], [25, 24] ]

Now I want to remove some pairs of coordinates from the array. Let's say I don't need [31, 82]. How do I remove this element from the array? I tried this :

var coords = [31, 82];

let index = arr.indexOf(coords);
                if (index !== -1) arr.splice(index, 1);

But it doesn't work.

4 Answers

you cannot use indexof in this scenario. Use custom function to do so,

    var array = [ [5, 34],  [31, 82], [10, 31], [25, 24] ]
    
    function indexOf(array, item) {
        for (var i = 0; i < array.length; i++) {
            // This if statement depends on the format of your array
            if (array[i][0] == item[0] && array[i][1] == item[1]) {
                return i;   // Found it
            }
        }
        return -1;   // Not found
    }

    console.log(indexOf(array, [31,82]))

Then you can splice it by index.

    var array = [ [5, 34],  [31, 82], [10, 31], [25, 24] ]
    
    function remove(array, item) {
        for (var i = 0; i < array.length; i++) {
            // This if statement depends on the format of your array
            if (array[i][0] == item[0] && array[i][1] == item[1]) {
                array.splice(i, 1);
                return array
            }
        }
        return array;   // Not found
    }

    console.log(remove(array, [31,82]))

Edited

You can also remove element then and there you found it :

You can simply do this by iterating over the array and comparing the corresponding values. Please find below the working code:

var array = [ [5, 34],  [31, 82], [10, 31], [25, 24] ]

var coords = [31, 82];

const newarr = array.filter((item) => {
  return !(item[0] == coords[0] && item[1] === coords[1]);
});

console.log(newarr);

Remove and stop to the first element found

If you want remove only the first element find, you can use every and return false to stop the loop. ;)

0, 1 -> stop

// usage: <array>.every(removeFirstCoordFound, <coord>)

function removeFirstCoordFound(val, i, arr, coord) {
  if (val[0] === this[0] && val[1] === this[1]) {
    return !arr.splice(i, 1)
  }
  return true;
}

Demo

const array = [ [5, 34],  [31, 82], [10, 31], [25, 24] ]
const coord = [31, 82];

console.log(JSON.stringify(array))

console.log('search', JSON.stringify(coord))

array.every(removeFirstCoordFound, coord)

function removeFirstCoordFound(val, i, arr, coord) {
  console.log(i)
  if (val[0] === this[0] && val[1] === this[1]) {
    return !arr.splice(i, 1)
  }
  return true
}

console.log('stop')

console.log(JSON.stringify(array))

Minified version:

array.every((v,i)=>!(v[0]==coord[0]&&v[1]==coord[1]&&!array.splice(i,1))

Please try this code to,how to remove an array from a 2D array?

function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line
  for(var i=0; i<arr.length; i++){
    if(arr[i].indexOf(elem) === -1){
      for(var j=0; j<arr[i].length; j++){
        if(arr[i][j] === elem){
          arr.splice((i,j), 1);
        }
      }
      newArr.push(arr[i]);
    }
    else{
      newArr.push(arr[i]);
    }
  }
  // change code above this line
  return newArr;
}

i hope this code will be usefull for you. Thank you.

Related