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

javascript - 将数组元素从一个数组位置移动到另一数组位置(Move an array element from one array position to another)

I'm having a hard time figuring out how to move an array element.(我很难弄清楚如何移动数组元素。)

For example, given the following:(例如,给出以下内容:)
var arr = [ 'a', 'b', 'c', 'd', 'e'];

How can I write a function to move 'd' before 'b' ?(我为什么能写入移动功能'd'之前, 'b' ?)

Or 'a' after 'c' ?(或'a''c' ?)

After the move, the indices of the rest of the elements should be updated.(移动之后,应更新其余元素的索引。)

This means in the first example after the move arr[0] would = 'a', arr[1] = 'd' arr[2] = 'b', arr[3] = 'c', arr[4] = 'e'(这意味着在第一个示例中,移动后arr [0] ='a',arr [1] ='d'arr [2] ='b',arr [3] ='c',arr [4] = 'e')

This seems like it should be pretty simple, but I can't wrap my head around it.(这看起来应该很简单,但是我无法将其包裹住。)

  ask by Mark Brown translate from so

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

1 Answer

0 votes
by (71.8m points)

If you'd like a version on npm, array-move is the closest to this answer, although it's not the same implementation.(如果您想在npm上使用一个版本, array-move是最接近此答案的方法,尽管它的实现方式不同。)

See its usage section for more details.(有关更多详细信息,请参见其用法部分。) The previous version of this answer (that modified Array.prototype.move) can be found on npm at array.prototype.move .(可以在npm的array.prototype.move上找到此答案的先前版本(修改后的Array.prototype.move)。)

I had fairly good success with this function:(使用此功能,我取得了相当不错的成功:)

 function array_move(arr, old_index, new_index) { if (new_index >= arr.length) { var k = new_index - arr.length + 1; while (k--) { arr.push(undefined); } } arr.splice(new_index, 0, arr.splice(old_index, 1)[0]); return arr; // for testing }; // returns [2, 1, 3] console.log(array_move([1, 2, 3], 0, 1)); 

Note that the last return is simply for testing purposes: splice performs operations on the array in-place, so a return is not necessary.(请注意,最后一次return只是出于测试目的: splice对数组就地执行操作,因此不需要返回。)

By extension, this move is an in-place operation.(通过扩展, move是就地操作。) If you want to avoid that and return a copy, use slice .(如果要避免这种情况并返回副本,请使用slice 。)

Stepping through the code:(逐步执行代码:)

  1. If new_index is greater than the length of the array, we want (I presume) to pad the array properly with new undefined s.(如果new_index大于数组的长度,我们(我想)要使用新的undefined正确填充数组。) This little snippet handles this by pushing undefined on the array until we have the proper length.(这个小片段通过在数组上推送undefined直到我们拥有适当的长度来处理此问题。)
  2. Then, in arr.splice(old_index, 1)[0] , we splice out the old element.(然后,在arr.splice(old_index, 1)[0] ,我们拼接出旧元素。) splice returns the element that was spliced out, but it's in an array.(splice返回被拼接的元素,但是它在数组中。) In our above example, this was [1] .(在我们上面的示例中,这是[1] 。) So we take the first index of that array to get the raw 1 there.(因此,我们采用该数组的第一个索引来获取原始1 。)
  3. Then we use splice to insert this element in the new_index's place.(然后,我们使用splice在new_index的位置插入此元素。) Since we padded the array above if new_index > arr.length , it will probably appear in the right place, unless they've done something strange like pass in a negative number.(由于如果new_index > arr.length填充了上面的数组,除非它做了一些奇怪的事情,例如传递负数,否则它可能会出现在正确的位置。)

A fancier version to account for negative indices:(解释负指数的更高级的版本:)

 function array_move(arr, old_index, new_index) { while (old_index < 0) { old_index += arr.length; } while (new_index < 0) { new_index += arr.length; } if (new_index >= arr.length) { var k = new_index - arr.length + 1; while (k--) { arr.push(undefined); } } arr.splice(new_index, 0, arr.splice(old_index, 1)[0]); return arr; // for testing purposes }; // returns [1, 3, 2] console.log(array_move([1, 2, 3], -1, -2)); 

Which should account for things like array_move([1, 2, 3], -1, -2) properly (move the last element to the second to last place).(哪个应该正确地解决诸如array_move([1, 2, 3], -1, -2) (将最后一个元素移动到倒数第二个位置)。)

Result for that should be [1, 3, 2] .(结果应为[1, 3, 2] 。)

Either way, in your original question, you would do array_move(arr, 0, 2) for a after c .(无论哪种方式,在你原来的问题,你会怎么做array_move(arr, 0, 2)ac 。)

For d before b , you would do array_move(arr, 3, 1) .(对于db之前,您可以执行array_move(arr, 3, 1) 。)

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

...