在javascript中从多维数组中删除列 [英] Deleting a column from a multidimensional array in javascript

查看:113
本文介绍了在javascript中从多维数组中删除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2D数组:

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.

谢谢!

推荐答案

尝试使用 。它不会改变对原始数组的任何更改

var x = array.map(function(val){
    return val.slice(0, -1);
});

console.log(x); // [[a,b],[a,b],[a,b]]

这篇关于在javascript中从多维数组中删除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆