当我使用Array.prototype.copyWithin方法时:
[1, 2, 3, 4, 5].copyWithin(-3,0,-1);
Run Code Online (Sandbox Code Playgroud)
输出是
[1, 2, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
数字1、2、3如何添加到输出结果中?
你有什么
[1, 2, 3, 4, 5].copyWithin(-3, 0, -1); // [1, 2, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
你做什么
Run Code Online (Sandbox Code Playgroud)[1, 2, 3, 4, 5] values 0 1 2 3 4 indices from start -5 -4 -3 -2 -1 indices from end ^ ^ ^ needed indices | | +-------- end | +-------------- target +-------------------- start [1, 2, 3, 4, 5] given array [ 1, 2, 3, 4, 5] copied values [1, 2, 1, 2, 3] result array, keeping the same length
您需要更改开始值和结束值。
基本上你有 4 种可能性,要么从头开始,要么从最后开始获取索引。
Run Code Online (Sandbox Code Playgroud)[1, 2, 3, 4, 5] values 0 1 2 3 4 indices from start -5 -4 -3 -2 -1 indices from end ^ ^ ^ needed indices | | +------- target | +---------- end +------------- start
[1, 2, 3, 4, 5].copyWithin(-3, 0, -1); // [1, 2, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
777 次 |
| 最近记录: |