Đảo ngược mảng con JavaScript

Một câu hỏi phỏng vấn hoặc thử thách JavaScript điển hình liên quan đến việc đảo ngược mảng tại chỗ và không đúng chỗ. Cụ thể, hãy giải quyết vấn đề này từ Eloquent JavaScript, 2nd Edition

Write two functions reverseArray and reverseArrayInPlace. The first, reverseArray, takes an array as an argument and produces a new array that has the same elements in the inverse order. The second, reverseArrayInPlace, does what the reverse method does: it modifies the array given as argument in order to reverse its elements. Neither may use the standard reverse method.

Đó là người khởi xướng. bạn không thể sử dụng phương pháp

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
5

Tôi đã giải quyết vấn đề này trước đây, nhưng bây giờ tôi đang quay lại học JavaScript sau một thời gian nghỉ ngơi, tôi phải mất một lúc để hiểu lại vấn đề này. Đặc biệt đảo ngược mảng tại chỗ. Để giúp tôi hiểu rõ hơn và suy nghĩ thấu đáo vấn đề này tôi quyết định viết bài này

Đảo ngược một mảng Out-of-Place

Để bắt đầu, hãy tạo hai mảng

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];

Đối với hàm

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
6, nếu chúng ta định tạo một mảng mới, chúng ta cần một mảng trống để điền vào sau

var newArray = [];

Viết các bước ra bằng mã giả

// 1) I want to take the last element of the array, then add it to the newArray. 
// 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
// 3) I want to output the contents of 'newArray' when the for loop is finished.

Trước tiên, hãy đến với vòng lặp

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
7 của chúng tôi. Để bắt đầu ở cuối mảng, chúng tôi muốn đặt
var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
8 bằng độ dài của mảng trừ đi 1, vì chỉ số của một mảng luôn bắt đầu từ 0

________số 8_______

Ví dụ,

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
9 sẽ cho chúng ta 7 vì có bảy phần tử trong đó. Tuy nhiên, chỉ số của mỗi là 0, 1, 2, 3, 4, 5 và 6. Vì vậy, để bắt đầu từ phần tử có chỉ số là 6, chúng ta cần lấy độ dài của mảng và trừ đi 1

Viết ra vòng lặp

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
7 đầy đủ của chúng tôi, chúng tôi nhận được

for (var i = arr.length - 1; i >= 0; i--)

Nói cách khác, chúng ta đang trừ 1 từ chỉ mục với mỗi vòng lặp cho đến khi

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
8 lớn hơn hoặc bằng 0, phần đầu của mảng. (Sử dụng
var newArray = [];
2 làm tên tham số trong hàm bên dưới cũng giống như sử dụng
var newArray = [];
3. Đừng để điều đó ném bạn. )

function reverseArray(arr) {
var newArray = [];
for (var i = arr.length - 1; i >= 0; i--) {
newArray.push(arr[i]);
}
return newArray;
}

var newArray = [];
4 đại diện cho từng phần tử tại chỉ mục tương ứng của nó. Ở vòng lặp đầu tiên,
var newArray = [];
4 là phần tử
var newArray = [];
6. Trên mỗi vòng lặp, chúng tôi sử dụng phương thức
var newArray = [];
7 để 'đẩy'
var newArray = [];
4 theo nghĩa đen đến cuối của
var newArray = [];
9

Truyền

// 1) I want to take the last element of the array, then add it to the newArray. 
// 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
// 3) I want to output the contents of 'newArray' when the for loop is finished.
0 vào hàm này, chúng ta sẽ nhận được

reverseArray(array1)⊳ ["if", "never", "sometimes", "always", "maybe", "no", "yes"]

Đảo ngược một mảng tại chỗ

Mọi thứ ngay lập tức trở nên phức tạp hơn đối với tôi khi cố gắng đảo ngược một mảng tại chỗ. Ban đầu, khi tôi cố gắng giải quyết vấn đề này, tôi đã cố gắng lấy phần tử đầu tiên bằng cách sử dụng

// 1) I want to take the last element of the array, then add it to the newArray. 
// 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
// 3) I want to output the contents of 'newArray' when the for loop is finished.
1, đặt phần tử đó thành một biến có tên là
// 1) I want to take the last element of the array, then add it to the newArray. 
// 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
// 3) I want to output the contents of 'newArray' when the for loop is finished.
2, sau đó sử dụng
var newArray = [];
7 để thêm nó vào cuối mảng ban đầu. Nhưng vấn đề mà tôi gặp phải là tôi chỉ đảo ngược phần tử đầu tiên và phần tử cuối cùng lặp đi lặp lại với mỗi vòng lặp, bởi vì
// 1) I want to take the last element of the array, then add it to the newArray. 
// 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
// 3) I want to output the contents of 'newArray' when the for loop is finished.
1 chỉ loại bỏ các phần tử ở 'chỉ số 0' và
// 1) I want to take the last element of the array, then add it to the newArray. 
// 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
// 3) I want to output the contents of 'newArray' when the for loop is finished.
5 ở cuối cùng

Ngoài ra còn có vấn đề về nơi bắt đầu và kết thúc vòng lặp

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
7

Tuy nhiên, trước khi đi xa hơn, hãy viết ra vấn đề bằng mã giả

// 1) I know I need to loop through the array. But before figuring out where to start or end I need to think about how I will actually reverse the elements in the array.
// 2) The only way I can think to do this is to switch the first element with the last element, then the second element with the second-to-last element, etc., until I reach the middle.

Vì vậy, về vòng lặp

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
7 của chúng tôi, tôi có thể bắt đầu ở đầu hoặc cuối mảng và kết thúc khi
var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
8 bằng một nửa chiều dài của mảng

Cách ban đầu tôi nghĩ để làm điều này là

for (var i = 0; i <= (arr.length / 2); i++)

Sau đó, tôi đặt phần tử đầu tiên bằng một biến có tên là

// 1) I want to take the last element of the array, then add it to the newArray. 
// 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
// 3) I want to output the contents of 'newArray' when the for loop is finished.
9, sau đó đặt phần tử đầu tiên bằng phần tử cuối cùng và phần tử cuối cùng bằng phần tử đầu tiên (
// 1) I want to take the last element of the array, then add it to the newArray. 
// 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
// 3) I want to output the contents of 'newArray' when the for loop is finished.
9)

Đây là giao diện của nó

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
0

Vì vậy, với mỗi vòng lặp,

// 1) I want to take the last element of the array, then add it to the newArray. 
// 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
// 3) I want to output the contents of 'newArray' when the for loop is finished.
9 sẽ được đặt lại bằng phần tử tiếp theo trong mảng

var i = array.length - 1
2 chỉ là một cách nói

  1. Ở vòng lặp đầu tiên, chỉ số là độ dài của mảng, trừ đi 1, trừ đi giá trị của
    var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
    var array2 = [5,8,2,9,5,6,3,1];
    8, bằng 0. Sử dụng
    // 1) I want to take the last element of the array, then add it to the newArray. 
    // 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
    // 3) I want to output the contents of 'newArray' when the for loop is finished.
    0 làm ví dụ, nó sẽ là.
    var i = array.length - 1
    5.
    var i = array.length - 1
    6là phần tử cuối cùng của
    // 1) I want to take the last element of the array, then add it to the newArray. 
    // 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
    // 3) I want to output the contents of 'newArray' when the for loop is finished.
    0, là
    var newArray = [];
    6
  2. Trên vòng lặp thứ hai, nó sẽ là.
    var i = array.length - 1
    9. Do đó,
    for (var i = arr.length - 1; i >= 0; i--)
    0 sẽ là phần tử đứng thứ hai của
    // 1) I want to take the last element of the array, then add it to the newArray. 
    // 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
    // 3) I want to output the contents of 'newArray' when the for loop is finished.
    0, tức là
    for (var i = arr.length - 1; i >= 0; i--)
    2. Cứ lặp đi lặp lại cho đến khi đến giữa mảng

Cố gắng nó theo cách này, có vẻ như

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
1

Nhưng khi thử nghiệm điều này, có điều gì đó kỳ lạ xảy ra với các mảng có độ dài chẵn, chẳng hạn như

for (var i = arr.length - 1; i >= 0; i--)
3

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
2

9 và 5 được đảo ngược ở giữa

Trong trường hợp mảng của chúng ta là một số lẻ, chẳng hạn như

// 1) I want to take the last element of the array, then add it to the newArray. 
// 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
// 3) I want to output the contents of 'newArray' when the for loop is finished.
0, sử dụng
for (var i = arr.length - 1; i >= 0; i--)
5 sẽ hiệu quả, vì
var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
9 là 3. 5 và vòng lặp sẽ tiếp tục miễn là
var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
8 nhỏ hơn hoặc bằng 3. 5. Nhưng nếu độ dài của chúng ta là 8, như trong
for (var i = arr.length - 1; i >= 0; i--)
3, thì vòng lặp sẽ dừng ở chỉ số 4, thực tế là 1 chỉ số đã qua nơi chúng ta muốn dừng. Bởi vì chỉ mục bắt đầu từ 0, chúng tôi thực sự muốn dừng lại ở 3

Để khắc phục điều này, chúng ta có thể trừ 1 từ độ dài của mảng trước khi chia cho 2. Để có biện pháp tốt, chúng ta có thể ném

for (var i = arr.length - 1; i >= 0; i--)
9 ở phía trước để làm tròn số thập phân xuống số nguyên gần nhất

var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
var array2 = [5,8,2,9,5,6,3,1];
3

Xem lại

  • Chúng tôi đã bắt đầu ở đầu mảng trong vòng lặp
    var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
    var array2 = [5,8,2,9,5,6,3,1];
    7 của chúng tôi
  • Chúng tôi di chuyển qua mảng cho đến khi đạt được nửa điểm
  • Với mỗi vòng lặp, chúng tôi đặt phần tử tại
    var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
    var array2 = [5,8,2,9,5,6,3,1];
    8 — hoặc
    var newArray = [];
    4 — bằng một biến có tên là
    // 1) I want to take the last element of the array, then add it to the newArray. 
    // 2) To do this I'll need to loop through the array from the end to the beginning, because I want the last element to be first.
    // 3) I want to output the contents of 'newArray' when the for loop is finished.
    9
  • Sau đó, chúng tôi đặt phần tử đầu tiên bằng phần tử cuối cùng và phần tử cuối cùng bằng phần tử đầu tiên
  • Với mỗi vòng lặp tiếp theo, khi chúng tôi di chuyển vào bên trong, chúng tôi đã làm điều tương tự

Tôi là một tân binh JavaScript, vì vậy nếu bạn có cách nào hiệu quả hơn để thực hiện việc này, tôi rất muốn nhận được phản hồi từ bạn trong phần nhận xét bên dưới

Làm cách nào để đảo ngược mảng trong JavaScript?

Phương thức đảo ngược mảng JavaScript () .
Sự miêu tả. Phương thức đảo ngược mảng Javascript() đảo ngược phần tử của một mảng. .
cú pháp. Cú pháp của nó như sau - mảng. đảo ngược();
Giá trị trả về. Trả về giá trị đơn bị đảo ngược của mảng
Thí dụ. Hãy thử ví dụ sau. .
đầu ra. Mảng đảo ngược là. 3,2,1,0

Làm cách nào để đảo ngược một mảng mà không sử dụng một mảng khác trong JavaScript?

Đảo ngược một mảng bằng JavaScript .
Sử dụng phương pháp đảo ngược
Sử dụng vòng lặp For giảm dần
Sử dụng Phương thức Unshift()
Không sử dụng mảng mới hoặc phương thức reverse()

Làm cách nào để đảo ngược đối tượng trong JavaScript?

Phương thức đảo ngược JavaScript (). đối tượng mảng . e. phần tử mảng đầu tiên trở thành phần tử cuối cùng và phần tử cuối cùng trở thành phần tử đầu tiên. The reverse() method is used to reverse the order of the elements in an array, i.e. first array element becomes the last and the last becomes the first.

Làm cách nào để đảo ngược bản đồ trong JavaScript?

Bản đồ mảng tiến trình JavaScript đảo ngược .
Đảo ngược mảng trước Map() Vì JS đảo ngược mảng ban đầu, chúng ta cần tạo một bản sao mới mà chúng ta có thể đảo ngược và gọi map trên đó. .
Truy cập các phần tử theo thứ tự đảo ngược. const ánh xạ = mảng. .
Đảo ngược bản đồ () và giữ nguyên thứ tự