[英]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?所以如果我有数字 29。循环遍历这个数组并删除第二个元素是 29 的数组元素的最有效方法是什么? ie ["Dick", "29"]
即[“迪克”,“29”]
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. .filter 使用本机代码循环遍历您的数组。 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 https://onecompiler.com/javascript/3y9unh6vt
The answer by brbcoding deletes the content of the array element but does not delete it. brbcoding的回答删除了数组元素的内容,但不删除。 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/ 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.拼接删除一行(参数中的 1),您可以通过将 1 替换为 2 来删除 2 行。 i-- 减少数组的长度。
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'] ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.