Remove element from 2d array javascript

I'm having trouble cycling through a multidimensional array and deleting a specific element array. My multidimensional array looks a little like this:

myArray = [["Tom", "161"], ["Dick", "29"], ["Harry", "46"]];

So if I have the number 29. What's the most efficient way to cycle through this array and delete the array element who's second element is 29? i.e. ["Dick", "29"]

asked Jul 11, 2013 at 19:44

4

var myArray = [["Tom", "161"], ["Dick", "29"], ["Harry", "46"]];
var myNewArray = myArray.filter(function(item){ return item[1] != 29 })  

.filter uses native code to loop over your array. Building a new array could of course be more expensive than just cutting a part out of the old one, to be tested.

answered Jul 11, 2013 at 19:52

tomdemuyttomdemuyt

4,5221 gold badge31 silver badges59 bronze badges

2

myArray = [["Tom", "161"], ["Dick", "29"], ["Harry", "46"]];
for(var i = 0; i <= myArray.length - 1; i++){
    if(myArray[i][1] == "29"){
        myArray[i].splice(0,2);
    }
}
console.log(myArray);

// returns [ [ 'Tom', '161' ], [], [ 'Harry', '46' ] ]

answered Jul 11, 2013 at 19:58

brbcodingbrbcoding

13k2 gold badges35 silver badges51 bronze badges

const organizers = [
  {
    id: '83f58b20-011d-11ed-b71c-47e6c5dfe098',
    createdAt: '2022-07-11T13:29:39.794Z',
    updatedAt: '2022-07-11T13:29:39.794Z',
    organizerId: '83deced0-011d-11ed-b71c-47e6c5dfe098',
    tournamentId: '83b37910-011d-11ed-b71c-47e6c5dfe098',
    organizerName: null
  },
  {
    id: '83f58b21-011d-11ed-b71c-47e6c5dfe098',
    createdAt: '2022-07-11T13:29:39.794Z',
    updatedAt: '2022-07-11T13:29:39.794Z',
    organizerId: '83e18df0-011d-11ed-b71c-47e6c5dfe098',
    tournamentId: '83b37910-011d-11ed-b71c-47e6c5dfe098',
    organizerName: 'MANOJ ABCD'
  }
];

const removeorganizerId = "83e18df0-011d-11ed-b71c-47e6c5dfe098"

var myNewArray = organizers.filter(function(item){ return item.organizerId !== removeorganizerId });



console.log("myNewArray ===> ", myNewArray);

Check Demo Here

https://onecompiler.com/javascript/3y9unh6vt

answered Jul 13 at 10:11

Remove element from 2d array javascript

The answer by brbcoding deletes the content of the array element but does not delete it. Here is a way around that:

myArray = [["Tom", "161"], ["Dick", "29"], ["Harry", "46"]];
document.getElementById("a").innerHTML = myArray;
for(var i = 0; i <= myArray.length - 1; i++){
   if(myArray[i][1] == "29"){
       myArray.splice(i--,1);
}
}
document.getElementById("b").innerHTML = myArray;
console.log(myArray);

https://jsfiddle.net/tmzshopping/dfdveazk/

The splice removes one row (the 1 in the argument), you can remove 2 rows by replacing the 1 by 2. i-- reduces the length of the array.

answered Jan 27, 2018 at 13:29

   array_name = [[1,2,3],[],['Hi','hello','world']] 
    let temp = []
    array_name.forEach(element => {
      if (element != '' || `enter condition here`) {
        temp.push(element)
      }
    });
    
    array_name = temp; // array_name = [  [1,2,3], ['Hi','hello','world'] ] 

answered Jul 27, 2020 at 14:22

Remove element from 2d array javascript

Aditya parmarAditya parmar

11 gold badge1 silver badge2 bronze badges

Remove an element from an array in Javascript :

Most of the time we need to manipulate array objects like removing an element from an array. We can either remove an element and change the original array or we can create one different array by changing the original. There are several ways to remove one or more element from an array. Let’s go through each one of them one by one :

Remove one element using splice() and indexOf() :

splice() method can be used to remove one or multiple elements from an array. It takes two arguments: the first one is the index to start the deleting process and the second one is the number of elements you want to delete. So, in our case, it will be 1.splice() returns the elements deleted from the array. For example :

var originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log("Original array : ", originalArray);

var itemToRemove = 6;
var index = originalArray.indexOf(itemToRemove);

var removedItems = originalArray.splice(index, 1);

console.log("Removed items from the array", removedItems);

console.log("Final array after " + itemToRemove + " is removed", originalArray);

It will print the below output :

Original array :  [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Removed items from the array [ 6 ]
Final array after 6 is removed [ 1, 2, 3, 4, 5, 7, 8, 9 ]

As you can see that splice() mutates the original array. To calculate the index of an element in an array, we are using indexOf() method. If you don’t want to change the array elements, you should avoid using this method.

Remove element from 2d array javascript

Using slice()

slice() method is useful if you don’t want to change the original array. We will create two new arrays from the original array: the first one will hold all the elements to the left of the deleting element and the second one will hold all the elements to the right of that deleting element. Finally, we will concatenate both arrays to find out the final array.

var originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log("Original array : ", originalArray);

var itemToRemove = 6;
var index = originalArray.indexOf(itemToRemove);

var finalArray = originalArray.slice(index, originalArray.length);

console.log("Final array after " + itemToRemove + " is removed", finalArray);

Output :

Original array :  [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Final array after 6 is removed [ 6, 7, 8, 9 ]

Remove element from 2d array javascript

Remove one value using filter() :

filter() can be used to remove one value from an array in JavaScript. It will not change the original array. Based on one condition, it will create one different array filtering out the original array. Let’s try to implement it :

var originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log("Original array : ", originalArray);

var itemToRemove = 6;

var finalArray = originalArray.filter(item => item != itemToRemove);

console.log("Final array after " + itemToRemove + " is removed", finalArray);

It will print the below output :

Original array :  [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Final array after 6 is removed [ 1, 2, 3, 4, 5, 7, 8, 9 ]

Remove element from 2d array javascript

Removing multiple values from an array :

The easiest way to remove multiple elements from an array is to use filter() with a condition to check if the current element should be in the final array or not. The elements that we want to filter out should be saved in a different array. filter() will create one different array from the original array.

var originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log("Original array : ", originalArray);

var itemsToRemove = [1, 3, 5, 7, 9];

var finalArray = originalArray.filter(item => !itemsToRemove.includes(item));

console.log("Final array after " + itemsToRemove + " are removed", finalArray);

Output :

Original array :  [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Final array after 1,3,5,7,9 are removed [ 2, 4, 6, 8 ]

Remove element from 2d array javascript

Conclusion :

Removing elements from an array in Javascript is pretty straightforward. We have explained different ways to remove single and multiple elements from an array. Try to run the above example and if you have any queries or questions, drop a comment below.