Hướng dẫn how to use array in function javascript - cách sử dụng mảng trong hàm javascript

Sự mô tả

Trong JavaScript, các mảng không phải là nguyên thủy mà thay vào đó là các đối tượng ____38 với các đặc điểm cốt lõi sau:

Show
  • Các mảng JavaScript có thể thay thế được và có thể chứa hỗn hợp các loại dữ liệu khác nhau. (Khi các đặc điểm đó không mong muốn, thay vào đó, hãy sử dụng các mảng được đánh máy.) and can contain a mix of different data types. (When those characteristics are undesirable, use typed arrays instead.)
  • Các mảng JavaScript không phải là mảng liên kết và do đó, các phần tử mảng không thể được truy cập bằng cách sử dụng các chuỗi tùy ý làm chỉ mục, nhưng phải được truy cập bằng các số nguyên không âm (hoặc dạng chuỗi tương ứng của chúng) làm chỉ mục. and so, array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using nonnegative integers (or their respective string form) as indexes.
  • Các mảng JavaScript không được chỉ số bằng không: phần tử đầu tiên của một mảng là tại Index
    const fruits = ['Apple', 'Banana'];
    console.log(fruits.indexOf('Banana'));
    // 1
    
    9, phần thứ hai là tại Index
    const fruits = ['Apple', 'Banana'];
    
    fruits.includes('Banana'); // true
    fruits.includes('Cherry'); // false
    
    // If indexOf() doesn't return -1, the array contains the given item.
    fruits.indexOf('Banana') !== -1; // true
    fruits.indexOf('Cherry') !== -1; // false
    
    0, v.v.-và phần tử cuối cùng là giá trị của thuộc tính
    const fruits = ['Apple', 'Banana'];
    
    fruits.includes('Banana'); // true
    fruits.includes('Cherry'); // false
    
    // If indexOf() doesn't return -1, the array contains the given item.
    fruits.indexOf('Banana') !== -1; // true
    fruits.indexOf('Cherry') !== -1; // false
    
    1 của mảng trừ
    const fruits = ['Apple', 'Banana'];
    
    fruits.includes('Banana'); // true
    fruits.includes('Cherry'); // false
    
    // If indexOf() doesn't return -1, the array contains the given item.
    fruits.indexOf('Banana') !== -1; // true
    fruits.indexOf('Cherry') !== -1; // false
    
    0.
    : the first element of an array is at index
    const fruits = ['Apple', 'Banana'];
    console.log(fruits.indexOf('Banana'));
    // 1
    
    9, the second is at index
    const fruits = ['Apple', 'Banana'];
    
    fruits.includes('Banana'); // true
    fruits.includes('Cherry'); // false
    
    // If indexOf() doesn't return -1, the array contains the given item.
    fruits.indexOf('Banana') !== -1; // true
    fruits.indexOf('Cherry') !== -1; // false
    
    0, and so on — and the last element is at the value of the array's
    const fruits = ['Apple', 'Banana'];
    
    fruits.includes('Banana'); // true
    fruits.includes('Cherry'); // false
    
    // If indexOf() doesn't return -1, the array contains the given item.
    fruits.indexOf('Banana') !== -1; // true
    fruits.indexOf('Cherry') !== -1; // false
    
    1 property minus
    const fruits = ['Apple', 'Banana'];
    
    fruits.includes('Banana'); // true
    fruits.includes('Cherry'); // false
    
    // If indexOf() doesn't return -1, the array contains the given item.
    fruits.indexOf('Banana') !== -1; // true
    fruits.indexOf('Cherry') !== -1; // false
    
    0.
  • Các hoạt động sao chép mảng JavaScript tạo các bản sao nông. (Tất cả các hoạt động sao chép tích hợp tiêu chuẩn với bất kỳ đối tượng JavaScript nào tạo các bản sao nông, thay vì các bản sao sâu).. (All standard built-in copy operations with any JavaScript objects create shallow copies, rather than deep copies).

Người xây dựng

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
3

Tạo một đối tượng

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
8 mới.

Tính chất tĩnh

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
5

Trả về hàm tạo

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
8.

Phương pháp tĩnh

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
7

Tạo một thể hiện

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
8 mới từ một đối tượng giống như mảng hoặc đối tượng có thể lặp lại.

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
9

Trả về

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
0 nếu đối số là một mảng hoặc
const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
1 khác.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
2

Tạo một thể hiện

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
8 mới với một số lượng đối số thay đổi, bất kể số hoặc loại đối số.

Thuộc tính thể hiện

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
4

Phản ánh số lượng các yếu tố trong một mảng.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
5

Chứa các tên thuộc tính không được bao gồm trong tiêu chuẩn ECMAScript trước phiên bản ES2015 và bị bỏ qua cho các mục đích liên kết câu lệnh

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
6.

Phương pháp thể hiện

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
7

Trả về mục mảng tại chỉ mục đã cho. Chấp nhận số nguyên âm, được tính lại từ mục cuối cùng.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
8

Trả về một mảng mới là mảng gọi được nối với (các) mảng khác và/hoặc giá trị.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
9

Sao chép một chuỗi các phần tử mảng trong một mảng.

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
0

Trả về một đối tượng lặp mảng mới chứa các cặp khóa/giá trị cho mỗi chỉ mục trong một mảng.

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
1

Trả về

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
0 nếu mọi phần tử trong mảng gọi thỏa mãn chức năng kiểm tra.

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
3

Điền vào tất cả các phần tử của một mảng từ chỉ mục bắt đầu sang chỉ mục cuối với giá trị tĩnh.

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
4

Trả về một mảng mới chứa tất cả các phần tử của mảng gọi mà hàm lọc được cung cấp trả về

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
0.

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
6

Trả về giá trị của phần tử đầu tiên trong mảng thỏa mãn chức năng thử nghiệm được cung cấp hoặc

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
7 nếu không tìm thấy phần tử thích hợp.

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
8

Trả về chỉ số của phần tử đầu tiên trong mảng thỏa mãn chức năng thử nghiệm được cung cấp hoặc

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
9 nếu không tìm thấy phần tử thích hợp.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
0

Trả về giá trị của phần tử cuối cùng trong mảng thỏa mãn chức năng thử nghiệm được cung cấp hoặc

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
7 nếu không tìm thấy phần tử thích hợp.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
2

Trả về chỉ số của phần tử cuối cùng trong mảng thỏa mãn chức năng thử nghiệm được cung cấp hoặc

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
9 nếu không tìm thấy phần tử thích hợp.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
4

Trả về một mảng mới với tất cả các yếu tố mảng con được liên kết vào nó đệ quy lên đến độ sâu được chỉ định.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
5

Trả về một mảng mới được hình thành bằng cách áp dụng một hàm gọi lại đã cho cho từng phần tử của mảng gọi, sau đó làm phẳng kết quả theo một cấp.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
6

Gọi một hàm cho mỗi phần tử trong mảng gọi.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
7 Thử nghiệmExperimental

Nhóm các phần tử của một mảng thành một đối tượng theo các chuỗi được trả về bởi một hàm thử nghiệm.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
8 Thử nghiệmExperimental

Nhóm các phần tử của một mảng thành

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
9 theo các giá trị được trả về bởi hàm thử nghiệm.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = 2;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
0

Xác định xem mảng gọi có chứa giá trị hay không, trả về

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
0 hoặc
const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
1 nếu thích hợp.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = 2;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
3

Trả về chỉ mục đầu tiên (ít nhất) mà tại đó một phần tử nhất định có thể được tìm thấy trong mảng gọi.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = 2;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
4

Tham gia tất cả các yếu tố của một mảng thành một chuỗi.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = 2;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
5

Trả về một trình lặp mảng mới chứa các phím cho mỗi chỉ mục trong mảng gọi.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = 2;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
6

Trả về chỉ mục cuối cùng (lớn nhất) mà tại đó một phần tử nhất định có thể được tìm thấy trong mảng gọi hoặc

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
9 nếu không tìm thấy.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = 2;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
8

Trả về một mảng mới chứa kết quả gọi một hàm trên mọi phần tử trong mảng gọi.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = 2;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
9

Loại bỏ phần tử cuối cùng khỏi một mảng và trả về phần tử đó.

const fruits = ['Apple', 'Banana'];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple
0

Thêm một hoặc nhiều phần tử vào cuối một mảng và trả về

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 mới của mảng.

const fruits = ['Apple', 'Banana'];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple
2

Thực thi chức năng gọi lại "giảm" do người dùng cung cấp trên mỗi phần tử của mảng (từ trái sang phải), để giảm nó xuống một giá trị duy nhất.

const fruits = ['Apple', 'Banana'];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple
3

Thực thi chức năng gọi lại "giảm" do người dùng cung cấp trên mỗi phần tử của mảng (từ phải sang trái), để giảm nó xuống một giá trị duy nhất.

const fruits = ['Apple', 'Banana'];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple
4

Đảo ngược thứ tự của các yếu tố của một mảng tại chỗ. (Đầu tiên trở thành người cuối cùng, lần cuối cùng trở thành đầu tiên.)

const fruits = ['Apple', 'Banana'];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple
5

Loại bỏ phần tử đầu tiên khỏi một mảng và trả về phần tử đó.

const fruits = ['Apple', 'Banana'];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple
6

Trích xuất một phần của mảng gọi và trả về một mảng mới.

const fruits = ['Apple', 'Banana'];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple
7

Trả về

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
0 Nếu ít nhất một phần tử trong mảng gọi thỏa mãn chức năng kiểm tra được cung cấp.

const fruits = ['Apple', 'Banana'];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple
9

Sắp xếp các phần tử của một mảng tại chỗ và trả về mảng.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
00

Thêm và/hoặc loại bỏ các yếu tố khỏi một mảng.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
01

Trả về một chuỗi cục bộ đại diện cho mảng gọi và các yếu tố của nó. Ghi đè phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
02.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
03

Trả về một chuỗi đại diện cho mảng gọi và các phần tử của nó. Ghi đè phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
04.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
05

Thêm một hoặc nhiều phần tử vào mặt trước của một mảng và trả về

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 mới của mảng.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
07

Trả về một đối tượng lặp mảng mới chứa các giá trị cho mỗi chỉ mục trong mảng.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
08

Một bí danh cho phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
09 theo mặc định.

Ví dụ

Phần này cung cấp một số ví dụ về các hoạt động mảng phổ biến trong JavaScript.

Tạo một mảng

Ví dụ này hiển thị ba cách để tạo mảng mới: đầu tiên sử dụng ký hiệu theo nghĩa đen của mảng, sau đó sử dụng hàm tạo

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
3 và cuối cùng sử dụng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
11 để xây dựng mảng từ một chuỗi.

// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log(fruits.length);
// 2

// 'fruits2' array created using the Array() constructor.
const fruits2 = new Array('Apple', 'Banana');
console.log(fruits2.length);
// 2

// 'fruits3' array created using String.prototype.split().
const fruits3 = 'Apple, Banana'.split(', ');
console.log(fruits3.length);
// 2

Tạo một chuỗi từ một mảng

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
12 để tạo một chuỗi từ mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"

Truy cập một mục mảng theo chỉ mục của nó

Ví dụ này cho thấy cách truy cập các mục trong mảng

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13 bằng cách chỉ định số chỉ mục của vị trí của chúng trong mảng.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined

Tìm chỉ mục của một mục trong một mảng

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
15 để tìm vị trí (chỉ mục) của chuỗi
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
16 trong mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1

Kiểm tra xem một mảng có chứa một mục nào đó không

Ví dụ này cho thấy hai cách để kiểm tra xem mảng

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13 có chứa
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
16 và
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
20 không: đầu tiên với phương pháp
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
21, và sau đó với phương thức
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
15 để kiểm tra giá trị chỉ mục không phải là
const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
9.

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false

Nối một mục vào một mảng

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
24 để nối một chuỗi mới vào mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3

Xóa mục cuối cùng khỏi một mảng

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
26 để xóa mục cuối cùng khỏi mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13.

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange

Lưu ý:

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
26 chỉ có thể được sử dụng để loại bỏ mục cuối cùng khỏi một mảng. Để loại bỏ nhiều mục từ cuối một mảng, hãy xem ví dụ tiếp theo.
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
26 can only be used to remove the last item from an array. To remove multiple items from the end of an array, see the next example.

Xóa nhiều mục từ cuối một mảng

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
29 để loại bỏ 3 mục cuối cùng khỏi mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]

Cắt ngắn một mảng xuống chỉ là n mục đầu tiên của nó

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
29 để cắt giảm mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13 xuống chỉ còn 2 mục đầu tiên.

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = 2;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]

Xóa mục đầu tiên khỏi một mảng

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
33 để xóa mục đầu tiên khỏi mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13.

const fruits = ['Apple', 'Banana'];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple

Lưu ý:

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
33 chỉ có thể được sử dụng để loại bỏ mục đầu tiên khỏi một mảng. Để loại bỏ nhiều mục từ đầu một mảng, hãy xem ví dụ tiếp theo.
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
33 can only be used to remove the first item from an array. To remove multiple items from the beginning of an array, see the next example.

Xóa nhiều mục từ đầu một mảng

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
29 để loại bỏ 3 mục đầu tiên khỏi mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
0

Thêm một mục đầu tiên mới vào một mảng

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
38 để thêm, tại Index
const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
9, một mục mới cho mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13 - biến nó thành mục đầu tiên mới trong mảng.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
1

Xóa một mục duy nhất bằng chỉ mục

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
29 để xóa chuỗi
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
16 khỏi mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13 - bằng cách chỉ định vị trí chỉ mục của
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
16.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
2

Xóa nhiều mục bằng chỉ mục

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
29 để loại bỏ các chuỗi
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
16 và
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
47 khỏi mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13 - bằng cách chỉ định vị trí chỉ mục của
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
16, cùng với số lượng tổng số mục để xóa.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
3

Thay thế nhiều mục trong một mảng

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
29 để thay thế 2 mục cuối cùng trong mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13 bằng các mục mới.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
4

Lặp lại trên một mảng

Ví dụ này sử dụng vòng lặp

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
52 để lặp qua mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13, đăng nhập từng mục vào bảng điều khiển.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
5

Nhưng

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
52 chỉ là một trong nhiều cách để lặp lại bất kỳ mảng nào; Để biết thêm các cách, xem các vòng lặp và lặp lại, và xem tài liệu cho
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
55,
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
56,
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
57,
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
58,
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
59 và
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
60 - và xem ví dụ tiếp theo, sử dụng phương pháp
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
61.

Gọi một hàm trên mỗi phần tử trong một mảng

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
61 để gọi hàm trên mỗi phần tử trong mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13; Hàm làm cho mỗi mục được ghi vào bảng điều khiển, cùng với số chỉ mục của mục.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
6

Hợp nhất nhiều mảng với nhau

Ví dụ này sử dụng phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
64 để hợp nhất mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13 với mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
66, để tạo ra một mảng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
67 mới. Lưu ý rằng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13 và
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
66 vẫn không thay đổi.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
7

Sao chép một mảng

Ví dụ này cho thấy ba cách để tạo một mảng mới từ mảng

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
13 hiện có: đầu tiên bằng cách sử dụng cú pháp lan truyền, sau đó bằng cách sử dụng phương thức
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
71, sau đó bằng cách sử dụng phương thức
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
72.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
8

Tất cả các hoạt động sao chép mảng tích hợp (cú pháp lan truyền,

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
7,
const fruits = ['Apple', 'Banana'];
const removedItem = fruits.shift();
console.log(fruits);
// ["Banana"]
console.log(removedItem);
// Apple
6 và
const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
8) tạo ra các bản sao nông. Thay vào đó, nếu bạn muốn có một bản sao sâu của một mảng, bạn có thể sử dụng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
76 để chuyển đổi mảng thành chuỗi JSON và sau đó
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
77 để chuyển đổi chuỗi trở lại thành một mảng mới hoàn toàn độc lập với mảng gốc.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
9

Bạn cũng có thể tạo các bản sao sâu bằng phương pháp

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
78, có lợi thế cho phép các đối tượng có thể chuyển được trong nguồn được chuyển sang bản sao mới, thay vì chỉ nhân bản.

Cuối cùng, điều quan trọng là phải hiểu rằng việc gán một mảng hiện có cho một biến mới không tạo ra một bản sao của mảng hoặc các phần tử của nó. Thay vào đó, biến mới chỉ là một tham chiếu, hoặc bí danh, cho mảng gốc; Đó là, tên của mảng gốc và tên biến mới chỉ là hai tên cho cùng một đối tượng (và do đó sẽ luôn đánh giá là tương đương nghiêm ngặt). Do đó, nếu bạn thực hiện bất kỳ thay đổi nào theo giá trị của mảng gốc hoặc với giá trị của biến mới, thì cũng sẽ thay đổi:

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
0

Nhóm các yếu tố của một mảng

Các phương thức

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
7 có thể được sử dụng để nhóm các phần tử của một mảng, sử dụng hàm thử nghiệm trả về một chuỗi biểu thị nhóm của phần tử hiện tại.

Ở đây chúng tôi có một mảng hàng tồn kho đơn giản có chứa các đối tượng "thực phẩm" có

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
80 và
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
81.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
1

Để sử dụng

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
82, bạn cung cấp chức năng gọi lại được gọi với phần tử hiện tại và tùy chọn chỉ mục và mảng hiện tại và trả về một chuỗi chỉ ra nhóm của phần tử.

Mã bên dưới sử dụng hàm mũi tên để trả về

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
81 của mỗi phần tử mảng (điều này sử dụng cú pháp phá hủy đối tượng cho các đối số chức năng để giải nén phần tử
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
81 khỏi đối tượng được truyền). Kết quả là một đối tượng có các thuộc tính được đặt tên theo các chuỗi duy nhất được trả về bởi cuộc gọi lại. Mỗi thuộc tính được gán một mảng chứa các phần tử trong nhóm.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
2

Lưu ý rằng đối tượng được trả về tham chiếu các phần tử giống như mảng gốc (không phải bản sao sâu). Thay đổi cấu trúc bên trong của các yếu tố này sẽ được phản ánh trong cả mảng gốc và đối tượng được trả về.

Ví dụ: nếu bạn không thể sử dụng chuỗi làm khóa làm khóa, nếu thông tin đến nhóm được liên kết với một đối tượng có thể thay đổi, thì thay vào đó bạn có thể sử dụng

const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
8. Điều này rất giống với
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
86 ngoại trừ việc nó nhóm các phần tử của mảng thành
const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice(start);
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItems);
// ["Strawberry", "Mango", "Cherry"]
9 có thể sử dụng giá trị tùy ý (đối tượng hoặc nguyên thủy) làm chìa khóa.

Những ví dụ khác

Tạo một mảng hai chiều

Sau đây tạo ra một bàn cờ như một chuỗi hai chiều. Động thái đầu tiên được thực hiện bằng cách sao chép

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
88 trong
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
89 lên
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
90. Vị trí cũ tại
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
91 được làm trống.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
3

Đây là đầu ra:

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
4

Sử dụng một mảng để lập bảng một tập hợp các giá trị

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
5

Kết quả trong

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
6

Tạo một mảng bằng kết quả của một trận đấu

Kết quả của một trận đấu giữa

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
92 và chuỗi có thể tạo ra một mảng JavaScript có thuộc tính và các phần tử cung cấp thông tin về trận đấu. Một mảng như vậy được trả về bởi
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
93 và
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
94.

Ví dụ:

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
7

Để biết thêm thông tin về kết quả của một trận đấu, hãy xem trang

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
93 và
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
94.

Ghi chú

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
8 Các đối tượng không thể sử dụng các chuỗi tùy ý làm chỉ số phần tử (như trong một mảng kết hợp) nhưng phải sử dụng các số nguyên không âm (hoặc dạng chuỗi tương ứng của chúng). Cài đặt hoặc truy cập thông qua những người không phải là thông tin sẽ không đặt hoặc truy xuất một phần tử từ chính danh sách mảng, nhưng sẽ đặt hoặc truy cập một biến được liên kết với bộ sưu tập thuộc tính đối tượng của mảng đó. Các thuộc tính đối tượng của mảng và danh sách các phần tử mảng là riêng biệt và các hoạt động di chuyển và đột biến của mảng không thể được áp dụng cho các thuộc tính được đặt tên này.

Các phần tử mảng là các thuộc tính đối tượng giống như cách

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
98 là một thuộc tính (tuy nhiên, cụ thể,
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
99 là một phương thức). Tuy nhiên, cố gắng truy cập một phần tử của một mảng như sau đã ném lỗi cú pháp vì tên thuộc tính không hợp lệ:

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
8

Cú pháp JavaScript yêu cầu các thuộc tính bắt đầu bằng một chữ số được truy cập bằng ký hiệu khung thay vì ký hiệu DOT. Cũng có thể trích dẫn các chỉ số mảng (ví dụ:

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
00 thay vì
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
01), mặc dù thường không cần thiết.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
02 trong
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
01 bị ép buộc thành một chuỗi bởi công cụ JavaScript thông qua chuyển đổi
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
98 ngầm. Do đó,
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
05 và
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
06 sẽ đề cập đến hai vị trí khác nhau trên đối tượng
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
07 và ví dụ sau đây có thể là
const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
0:

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
9

Chỉ

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
00 là một chỉ số mảng thực tế.
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
10 là một thuộc tính chuỗi tùy ý sẽ không được truy cập trong vòng lặp mảng.

Mối quan hệ giữa độ dài và tính chất số

Thuộc tính

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 của mảng JavaScript được kết nối.

Một số phương thức mảng tích hợp (ví dụ:

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
12,
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
72,
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
15, v.v.) có tính đến giá trị của thuộc tính
const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 của mảng khi chúng được gọi.

Các phương thức khác (ví dụ:

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
24,
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
29, v.v.) cũng dẫn đến các bản cập nhật cho thuộc tính
const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 của mảng.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
0

Khi đặt thuộc tính trên mảng JavaScript khi thuộc tính là chỉ mục mảng hợp lệ và chỉ mục đó nằm ngoài giới hạn hiện tại của mảng, động cơ sẽ cập nhật thuộc tính

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 của mảng cho phù hợp:

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
1

Tăng

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
2

Giảm thuộc tính

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1, tuy nhiên, xóa các yếu tố.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
3

Điều này được giải thích thêm trên trang

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
22.

Phương pháp mảng và các khe trống

Các khe trống trong các mảng thưa thớt cư xử không nhất quán giữa các phương thức mảng. Nói chung, các phương pháp cũ hơn sẽ bỏ qua các khe trống, trong khi các phương pháp mới hơn coi chúng là

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
7.

Trong số các phương thức lặp lại thông qua nhiều yếu tố, sau đây thực hiện kiểm tra

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
24 trước khi truy cập chỉ mục và không kết hợp các khe trống với
const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
7:

  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    64
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    27
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    55
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    56
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    30
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    57
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    61
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    15
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    34
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    58
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    59
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    60
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    38
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    72
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    40
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    41
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    29

Để biết chính xác cách họ xử lý các khe trống, hãy xem trang cho mỗi phương thức.

Các phương pháp này xử lý các khe trống như thể chúng là

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
7:

  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    44
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    45
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    46
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    47
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    48
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    49
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    82
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    51
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    21
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    12
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    54
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    55
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    09

Sao chép phương pháp và phương pháp đột biến

Một số phương thức không làm thay đổi mảng hiện tại mà phương thức đã được gọi, mà thay vào đó lại trả về một mảng mới. Họ làm như vậy bằng cách trước tiên truy cập

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
57 để xác định hàm tạo để sử dụng cho mảng mới. Mảng mới được xây dựng sau đó được điền với các yếu tố. Bản sao luôn xảy ra nông cạn - phương thức không bao giờ sao chép bất cứ thứ gì ngoài mảng được tạo ban đầu. Các phần tử của (các) mảng gốc được sao chép vào mảng mới như sau:

  • Đối tượng: Tham chiếu đối tượng được sao chép vào mảng mới. Cả mảng gốc và mảng mới đều đề cập đến cùng một đối tượng. Đó là, nếu một đối tượng được tham chiếu được sửa đổi, các thay đổi được hiển thị cho cả mảng mới và ban đầu.
  • Các loại nguyên thủy như chuỗi, số và booleans (không phải
    const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    58,
    const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    59 và
    const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    60 đối tượng): Giá trị của chúng được sao chép vào mảng mới.

Các phương thức khác làm biến đổi mảng mà phương thức được gọi, trong trường hợp giá trị trả về của chúng khác nhau tùy thuộc vào phương thức: đôi khi một tham chiếu đến cùng một mảng, đôi khi độ dài của mảng mới.

Các phương thức sau tạo các mảng mới với

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
61:

  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    64
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    56
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    30
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    57
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    58
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    72
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    29 (để xây dựng mảng các phần tử bị loại bỏ được trả về)

Lưu ý rằng

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
82 và
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
51 không sử dụng
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
61 để tạo các mảng mới cho mỗi mục nhập nhóm, nhưng luôn sử dụng hàm tạo
const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
8 đơn giản. Về mặt khái niệm, họ cũng không sao chép các phương pháp.

Các phương pháp sau đây làm biến đổi mảng ban đầu:

  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    27
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    45
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    26
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    24
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    38
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    33
  • const fruits = ['Apple', 'Banana'];
    
    // The index of an array's first element is always 0.
    fruits[0]; // Apple
    
    // The index of an array's second element is always 1.
    fruits[1]; // Banana
    
    // The index of an array's last element is always one
    // less than the length of the array.
    fruits[fruits.length - 1]; // Banana
    
    // Using a index number larger than the array's length
    // returns 'undefined'.
    fruits[99]; // undefined
    
    41
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    29
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    38

Phương pháp mảng chung

Các phương thức mảng luôn chung chung - chúng không truy cập bất kỳ dữ liệu nội bộ nào của đối tượng mảng. Họ chỉ truy cập các phần tử mảng thông qua thuộc tính

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 và các phần tử được lập chỉ mục. Điều này có nghĩa là chúng cũng có thể được gọi trên các đối tượng giống như mảng.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
4

Bình thường hóa thuộc tính chiều dài

Thuộc tính

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 được chuyển đổi thành một số, bị cắt thành một số nguyên và sau đó được kẹp đến phạm vi từ 0 đến 253 - 1.
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
84 trở thành
const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
9, vì vậy ngay cả khi
const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 không có mặt hoặc là
const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
7, nó hoạt động như thể nó có giá trị .

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
5

Một số phương thức mảng đặt thuộc tính

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 của đối tượng mảng. Họ luôn đặt giá trị sau khi chuẩn hóa, do đó
const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 luôn kết thúc như một số nguyên.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
6

Các đối tượng giống như mảng

Thuật ngữ đối tượng giống như mảng đề cập đến bất kỳ đối tượng nào không ném trong quá trình chuyển đổi

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 được mô tả ở trên. Trong thực tế, đối tượng như vậy dự kiến ​​sẽ thực sự có thuộc tính
const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1 và có các phần tử được lập chỉ mục trong phạm vi
const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
9 đến
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
94. (Nếu nó không có tất cả các chỉ số, nó sẽ tương đương về mặt chức năng với một mảng thưa thớt.)

Nhiều đối tượng DOM giống như mảng-ví dụ,

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
95 và
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
96. Đối tượng
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
97 cũng giống như mảng. Bạn có thể gọi các phương thức mảng trên chúng ngay cả khi chúng không có các phương thức này.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
7

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # sec-marray-expects
# sec-array-objects

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Làm thế nào để bạn chuyển một mảng cho một hàm trong javascript?

Để chuyển một đối số mảng cho một hàm, chỉ cần chuyển tên của một mảng (một tham chiếu đến một mảng) mà không có dấu ngoặc. Ví dụ: nếu chúng ta đã khai báo một mảng dấu là: LET HOẠT ĐỘNGTEMP = Mảng mới (30);pass the name of an array (a reference to an array) without brackets. For example, if we have declared an array marks as: let hourlyTemp = new Array(30);

Làm thế nào để bạn mang một mảng đến một chức năng?

Để chuyển toàn bộ một mảng cho một hàm, chỉ tên của mảng được truyền như một đối số.kết quả = tính toán (num);Tuy nhiên, lưu ý việc sử dụng [] trong định nghĩa chức năng.Điều này thông báo cho trình biên dịch rằng bạn đang chuyển một mảng một chiều cho hàm.only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.

Một hàm có thể trả về một mảng javascript?

Các hàm JavaScript có thể trả về một giá trị duy nhất.Để trả về nhiều giá trị từ một hàm, bạn có thể đóng gói các giá trị trả về dưới dạng các phần tử của một mảng hoặc là thuộc tính của một đối tượng.you can pack the return values as elements of an array or as properties of an object.

Mảng là gì [

Truy cập mảng [-1] Trong một số ngôn ngữ lập trình, chúng ta có thể truy cập các phần tử mảng bằng các chỉ mục âm, được tính từ cuối.Giống như thế này: hãy để mảng = [1, 2, 3];mảng [-1];// 3, mảng phần tử cuối cùng [-2];// 2, một bước từ mảng cuối [-3];// 1, hai bước từ cuối.access array elements using negative indexes, counted from the end. Like this: let array = [1, 2, 3]; array[-1]; // 3, the last element array[-2]; // 2, one step from the end array[-3]; // 1, two steps from the end.