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"]
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"]
Share Improve this question asked Jul 11, 2013 at 19:44 aaduaadu 3,25410 gold badges42 silver badges65 bronze badges 4 |5 Answers
Reset to default 12var 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.
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' ] ]
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
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.
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'] ]
for
loop. Depending on what else you are doing with the data, you could sort the array first. – Felix Kling Commented Jul 11, 2013 at 19:45indexOf
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. – Felix Kling Commented Jul 11, 2013 at 19:48