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
237 views
in Technique[技术] by (71.8m points)

javascript - 如何从JavaScript中删除数组中的元素?(How to remove element from an array in JavaScript?)

var arr = [1,2,3,5,6];

I want to remove the 1st element of the array so that it becomes:

(我想删除数组的第一个元素,使其成为:)

var arr = [2,3,5,6];

To extend this question, what if I want to remove the 2nd element of the array so that it becomes:

(要扩展这个问题,如果我想删除数组的第二个元素,使其变为:)

var arr = [1,3,5,6];
  ask by user198729 translate from so

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

1 Reply

0 votes
by (71.8m points)

shift() is ideal for your situation.

(shift()非常适合您的情况。)

shift() removes the first element from an array and returns that element.

(shift()从数组中删除第一个元素并返回该元素。)

This method changes the length of the array.

(此方法更改数组的长度。)

array = [1, 2, 3, 4, 5];

array.shift(); // 1

array // [2, 3, 4, 5]

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

...