2

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.

Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
  • Have you had a look here: https://stackoverflow.com/questions/29070924/how-to-remove-row-in-2d-array-in-javascript – Gaurav Mall Mar 31 '20 at 09:57

4 Answers4

3

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 :

Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
  • +1 for keeping simplicity and also returning when found, that will surely improve performace of app, for this kind of scenario we must use for loop instead of filter or other array functions as they will loop through the entire array even if element found on 0 index – Kiran Shinde Mar 31 '20 at 10:09
3

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);
Jasdeep Singh
  • 7,901
  • 1
  • 11
  • 28
1

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))
user2226755
  • 12,494
  • 5
  • 50
  • 73
0

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.

Makwana Prahlad
  • 1,025
  • 5
  • 20