aadu
aadu

Reputation: 3254

Deleting specific array element from multidimensional array?

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"]

Upvotes: 6

Views: 19483

Answers (5)

Manoj Gohel
Manoj Gohel

Reputation: 73

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

Upvotes: 1

Aditya parmar
Aditya parmar

Reputation: 31

   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'] ] 

Upvotes: 0

TMZ
TMZ

Reputation: 43

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.

Upvotes: 0

brbcoding
brbcoding

Reputation: 13586

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' ] ]

Upvotes: 2

tomdemuyt
tomdemuyt

Reputation: 4592

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.

Upvotes: 12

Related Questions