最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Deleting specific array element from multidimensional array? - Stack Overflow

programmeradmin3浏览0评论

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
  • A 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:45
  • I thought it would be some combination of indexOf and splice? – aadu 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. – Felix Kling 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? – aadu Commented Jul 11, 2013 at 19:54
Add a comment  | 

5 Answers 5

Reset to default 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.

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'] ] 
发布评论

评论列表(0)

  1. 暂无评论