简体   繁体   English

JavaScript 倒车阵列

[英]JavaScript reversing array

 function reverseArrayInPlace(array) { for (let i = 0; i < Math.floor(array.length / 2); i++) { let old = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = old; } return array; } let arrayValue = [1, 2, 3, 4, 5]; reverseArrayInPlace(arrayValue); console.log(arrayValue); // → [5, 4, 3, 2, 1]

I am working on the Data Structures of the book Eloquent JavaScript.我正在研究 Eloquent JavaScript 这本书的数据结构。 I mostly understand how this loop works which is cutting the array in half and then start swapping elements at the opposite sides.我主要了解这个循环是如何工作的,它将数组切成两半,然后开始交换相对侧的元素。 but here my problem: the first round of the loop my understanding of "array[i]" is at index 0 and "Math.floor(array.length / 2)" is at index 1. So, we set old = index 0 and then override it to "array[array.length - 1 - i]".但这是我的问题:第一轮循环我对“array[i]”的理解位于索引 0,而“Math.floor(array.length / 2)”位于索引 1。所以,我们设置 old = index 0然后将其覆盖为“array [array.length - 1 - i]”。 the question is first what exactly does -i part mean and what index does this "array[array.length - 1 - i]" located at at the first round of the loop.问题首先是-i部分到底是什么意思,这个“array [array.length - 1 - i]”位于第一轮循环的索引是什么。 please someone explain it to I am very visual and I can't pass it if I don't see each statement in action.请有人向我解释一下,我非常直观,如果我没有看到每条语句在行动,我就无法通过它。

array.length - 1 - i is at the first round array.length - 1 - 0 which is the last element in the array. array.length - 1 - i在第一轮array.length - 1 - 0是数组中的最后一个元素。 So the loop swaps the first and the last element, then increments i .所以循环交换第一个和最后一个元素,然后递增i Next round the 2nd and the value before the last one is going to be swapped and so on.下一轮第二个和最后一个之前的值将被交换,依此类推。

Array:             1234567890
1st Iteration i=0: ^        ^
2nd Iteration i=1:  ^      ^
3rd Iteration i=2:   ^    ^
4th Iteration i=3:    ^  ^
5th Iteration i=4:     ^^

An array is zero-indexed.数组是零索引的。 Meaning, the first item in an array is at the [0] position.意思是,数组中的第一项位于 [0] position。 When you use array.length , it returns a total of array elements which can be confusing to you because an array of length 5 will have it's last element located at array[4] (because zero is the first).当您使用array.length时,它返回的数组元素总数可能会让您感到困惑,因为长度为 5 的数组的最后一个元素位于 array[4] (因为第一个元素为零)。

For each iteration in your function, it uses the counter i to find the position at the end of the array that matches the iteration.对于 function 中的每次迭代,它使用计数器i在与迭代匹配的数组末尾找到 position。

array.length - 1 - i

The -1 part is explained above.上面解释了-1部分。 When using array.length, you have to subtract 1 to get the actual position.使用array.length时,必须减去1才能得到实际的position。

easier to understand, and without the problem with odd arr.length更容易理解,并且没有奇数 arr.length 的问题

 function reverseArrayInPlace(arr) { for (let i= 0, j= arr.length -1; i < j; i++, j--) { // swapp values of arr[i] and arr[j] [ arr[i], arr[j] ] = [ arr[j], arr[i] ]; } return arr; } let arrayValues = [1, 2, 3, 4, 5]; reverseArrayInPlace( arrayValues ); document.write( JSON.stringify( arrayValues ));

I find the following approach helpful for working out what a loop is doing.我发现以下方法有助于弄清楚循环在做什么。 You start with the array 1,2,3,4,5.您从数组 1、2、3、4、5 开始。 Each iteration of the loop has two steps which change the array.循环的每次迭代都有两个更改数组的步骤。 The result of each step acting on the array are shown under 1st Step and 2nd Step below.作用在数组上的每个步骤的结果显示在下面的 1st Step 和 2nd Step 下。 Also shown is the value of i for the loop and the value of old for the loop.还显示了循环的 i 值和循环的 old 值。

i  old   1st Step    2nd Step
0  1     5,2,3,4,5   5,2,3,4,1
1  2     5,4,3,4,1   5,4,3,2,1

1st Step: array[i] = array[array.length - 1 - i];第一步: array[i] = array[array.length - 1 - i];

2nd Step: array[array.length - 1 - i] = old;第二步: array[array.length - 1 - i] = old;

Using two indexes into the array, one increasing from 0, the other decreasing from array.length-1, and with a destructuring swap, reverse can be stated a little more concisely like this...在数组中使用两个索引,一个从 0 增加,另一个从 array.length-1 减少,并且通过解构交换,可以更简洁地说明 reverse...

 function reverseArrayInPlace(array) { for (let i=0, j=array.length-1; i<j; i++, j--) [array[i], array[j]] = [array[j], array[i]]; return array; } let arrayValue = [1, 2, 3, 4, 5]; reverseArrayInPlace(arrayValue); console.log(arrayValue); // → [5, 4, 3, 2, 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM