本文介绍了从javascript中的多维数组中删除一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个二维数组:
var array = [["a", "b", "c"],["a", "b", "c"],["a", "b", "c"]]
我想删除该数组的一整列(即删除每个数组中的每三个元素).
I want to delete an entire column of this array (i.e. delete every third element within each array).
有解决方案这里和,但它们都不是在 javascript 中,所以我无法将情况联系起来.
There are solutions here and here, but neither of them is in javascript, so I'm having trouble relating the situations.
解决这个问题的最佳方法是什么?我不想使用 .splice() 因为在某些情况下我会删除多个列,而 .splice() 方法会改变数组的长度,所以我最终会越界访问.
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.
谢谢!
推荐答案
尝试使用 slice
.它不会改变对原始 array
var array = [["a", "b", "c"],["a", "b", "c"],["a", "b", "c"]]
var x = array.map(function(val) {
return val.slice(0, -1);
});
console.log(x); // [[a,b],[a,b],[a,b]]
这篇关于从javascript中的多维数组中删除一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!