[英]Deleting a column from a multidimensional array in javascript
I have a 2D array:我有一个二维数组:
var array = [["a", "b", "c"],["a", "b", "c"],["a", "b", "c"]]
I want to delete an entire column of this array (ie delete every third element within each array).我想删除这个数组的整个列(即删除每个数组中的每三个元素)。
There are solutions here and here , but neither of them is in javascript, so I'm having trouble relating the situations. 这里和这里都有解决方案,但它们都不在 javascript 中,所以我无法将这些情况联系起来。
What's the best way to approach this problem?解决这个问题的最佳方法是什么? I don't want to use.splice() because in some cases I'll be deleting multiple columns, and the.splice() method will change the length of the array, so I end up accessing out of bounds.我不想使用 .splice() 因为在某些情况下我会删除多列,而 .splice() 方法会改变数组的长度,所以我最终会越界访问。
Thanks!谢谢!
Iterate through the array and splice each sub array:遍历数组并拼接每个子数组:
var idx = 2;
for(var i = 0 ; i < array.length ; i++)
{
array[i].splice(idx,1);
}
Edit : I see you don't want to use splice
due to out-of-bounds problems and array length changing.编辑:我看到由于越界问题和数组长度变化,您不想使用splice
。 So:所以:
1.You can check if you're out of bounds and skip the slice. 1.您可以检查是否出界并跳过切片。
2.You can create an array of indexes you want to delete and simply create new arrays from the indexes that don't appear in that array (instead of deleting, create new arrays with the opposite condition). 2.您可以创建一个要删除的索引数组,然后简单地从该数组中没有出现的索引创建新数组(而不是删除,创建具有相反条件的新数组)。
Something like this:像这样的东西:
var array = [
["a", "b", "c"],
["a", "b", "c"],
["a", "b", "c"]
];
var idxToDelete = [0,2];
for (var i = 0; i < array.length; i++) {
var temp = array[i];
array[i] = [];
for(var j = 0 ; j < temp.length ; j++){
if(idxToDelete.indexOf(j) == -1) // dont delete
{
array[i].push(temp[j]);
}
}
}
This function doesn't use splice and it removes any column you want:此函数不使用 splice,它会删除您想要的任何列:
function removeEl(array, remIdx) {
return array.map(function(arr) {
return arr.filter(function(el,idx){return idx !== remIdx});
});
};
Hope this is what you are looking for希望这是你正在寻找的
use the map
function and splice
function for your solution.将map
函数和splice
函数用于您的解决方案。 like this像这样
var array = [["a", "b", "c"],["a", "b", "c"],["a", "b", "c"]];
array = array.map(function(item){
// the 0,2 tells the splice function to remove (skip) the last item in this array
return item.splice(0,2);
});
console.log(array);
// [["a", "b"],["a", "b"],["a", "b",]];
don't use delete
to delete items from a javascript array.不要使用delete
从 javascript 数组中删除项目。 the reason is that delete will toss the item but don't update the internal length variable.原因是 delete 会抛出项目但不更新内部长度变量。
example例子
var array = ["a", "b", "c"];
delete array[3];
console.log(array);
// ["a", "b"]
console.log(array.length);
// 3
array = array.splice(0,2);
console.log(array);
// ["a", "b"]
console.log(array.length);
// 2
use splice
this sets the correct length and delete the item of the array.使用splice
设置正确的长度并删除数组的项目。
You can use filter
to remove columns without the need to modify the original array.您可以使用filter
删除列,而无需修改原始数组。
var array = [["a", "b", "c"],["a", "b", "c"],["a", "b", "c"]]
var removeIndex = 1
const filtered = array.map(v => v.filter((_, i) => i !== 1))
// output: [["a", "c"], ["a", "c"], ["a", "c"]]
You can also remove multiple columns with the same approach.您还可以使用相同的方法删除多个列。
var array = [["a", "b", "c"],["a", "b", "c"],["a", "b", "c"]]
var removeIndexes = [0, 2]
const filtered = array.map(v => v.filter((_, i) => !removeIndexes.includes(i)))
// output: [["b"], ["b"], ["b"]]
splice
is cool. splice
很酷。 It resizes the array as it removes things so you don't end up with nulls.它在删除内容时调整数组的大小,因此您不会以空值结束。 So using splice you just have to iterate through each row and remove the right element.因此,使用 splice 您只需要遍历每一行并删除正确的元素。
var removeCol = function(arr2d, colIndex) {
for (var i = 0; i < arr2d.length; i++) {
var row = arr2d[i];
row.splice(colIndex, 1);
}
}
Useful code snipped that creates an independent copy of an array创建数组的独立副本的有用代码剪断
use slice
method inside map
.在map
使用slice
方法。 Here is an example:下面是一个例子:
let arrayA = ['a', 'b', 'c', 'd'] let arrayB = arrayA.map((val) => { return val.slice() }) arrayB[2] = 'Z'; console.log('--------- Array A ---------') console.log(arrayA) console.log('--------- Array B ---------') console.log(arrayB)
Note: In Angular 10
, I used different methods to get an independent copy of an array but all failed.注意:在Angular 10
,我使用了不同的方法来获取数组的独立副本,但都失败了。 Here are some of them:这里是其中的一些:
arrayB.push(arrayA) // failed
arrayB = Object.create(arrayA) // failed
arrayB = arrayA.splice() // failed
All failed methods were copying references along with data.所有失败的方法都在复制引用和数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.