6

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

4
  • A for loop. Depending on what else you are doing with the data, you could sort the array first. Commented Jul 11, 2013 at 19:45
  • I thought it would be some combination of indexOf and splice? Commented Jul 11, 2013 at 19:48
  • indexOf won't really work, since you are looking for an element inside the elements. But you can easily test what is faster with jsperf.com. Commented Jul 11, 2013 at 19:48
  • Yeah, that's what I thought which is why I was asking. But I'm not sure what the for loop should look like to achieve this? Commented Jul 11, 2013 at 19:54

5 Answers 5

12
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.

Sign up to request clarification or add additional context in comments.

2 Comments

That works, but you think making a new array is less efficient than the above loop and splice method?
This method is substanially faster than mine... jsperf.com/removearrfrommulti
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' ] ]

Comments

1
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

Comments

0

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.