Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

jquery - 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 (i.e. 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.

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.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try using slice. It won't alter any changes to your original 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]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...