Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 733
aadu Avatar asked Dec 20 '22 03:12

aadu


1 Answers

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.

like image 117
tomdemuyt Avatar answered May 25 '23 05:05

tomdemuyt