Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

js移动数组的顺序

如何将数组 ['a', 'b', 'c', 'd', 'e'] 中的 index==3移动到index==1中, 变成 ['a', 'd', 'b', 'c', 'e']
要求: 代码越简短越好


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

1 Answer

0 votes
by (71.8m points)
const arr = ['a', 'b', 'c', 'd', 'e'];

arr.splice(1, 0, arr.splice(3, 1)[0]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...