Hướng dẫn array not includes javascript - mảng không bao gồm javascript

Kiểm tra xem mảng không bao gồm giá trị #

Để kiểm tra xem một mảng không bao gồm một giá trị, hãy sử dụng toán tử logic không (!) Để phủ định cuộc gọi đến phương thức

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
1. Toán tử không (!) Trả về
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
2 khi được gọi trên giá trị
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
3 và ngược lại.

Show

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false if (notIncludesC) { console.log('✅ the value c is NOT included in the array'); } else { console.log('⛔️ the value c is included in the array'); }

Chúng tôi đã sử dụng toán tử logic không (!) Để phủ định các cuộc gọi đến phương thức mảng.

Cách tiếp cận này cho phép chúng tôi kiểm tra xem một giá trị cụ thể không có trong mảng.

Ví dụ đầu tiên của chúng tôi kiểm tra xem giá trị

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
4 không được chứa trong mảng và trả về
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
3.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false

Chuỗi

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
6 được chứa trong mảng, do đó biểu thức trả về
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
2.

Dưới đây là một số ví dụ khác về việc sử dụng toán tử logic không (!).

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true

Bạn có thể tưởng tượng rằng toán tử logic không (!) Trước tiên chuyển đổi giá trị thành

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
8 và sau đó lật giá trị.

Khi bạn phủ định giá trị giả, toán tử trả về

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
3. Trong tất cả các trường hợp khác, nó trả về
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
2.

Các giá trị giả trong JavaScript 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
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
2, chuỗi 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,
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 và
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
2.

Đọc thêm #

  • Phần tử mảng.push () nếu không tồn tại bằng cách sử dụng javascript
  • Kiểm tra xem mảng có chứa các phần tử trống trong javascript không
  • Kiểm tra xem mảng có chứa một đối tượng trong javascript không
  • Kiểm tra xem một mảng có chứa các bản sao trong JavaScript không
  • Kiểm tra xem mảng chỉ chứa các số trong javascript

Sự mô tả

Trong JavaScript, các mảng không phải là nguyên thủy mà thay vào đó 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
6 đối tượng với các đặc điểm cốt lõi sau:

  • 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ỉ định bằng 0: phần tử đầu tiên của một mảng là ở Index
    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, phần thứ hai là tại Index
    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, v.v.-và phần tử cuối cùng là giá trị của thuộc tính
    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 của mảng 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
    
    8.
    : the first element of an array is at index
    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, the second is at index
    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, and so on — and the last element is at the value of the array's
    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 property minus
    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á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'];
console.log(fruits.indexOf('Banana'));
// 1
1

Tạo một đố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
6 mới.

Tính chất tĩnh

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

Trả về hàm tạo

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.

Phương pháp tĩnh

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

Tạo một thể hiệ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
6 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'];
console.log(fruits.indexOf('Banana'));
// 1
7

Trả về

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

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

Tạo một thể hiệ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
6 mới với 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'];

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
2

Phản ánh số lượng các yếu tố trong một mả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

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 tuyên bố

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
4.

Phương pháp thể hiệ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
5

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'];

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
6

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'];

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

Sao chép một chuỗi các phần tử mảng trong một mả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
8

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'];

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 fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
3 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'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
1

Đ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'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
2

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 fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
3.

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

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'];

// 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 nếu không tìm thấy phần tử thích hợp.

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

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'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
7 nếu không tìm thấy phần tử thích hợp.

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

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'];

// 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 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
0

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'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
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
2

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', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
3

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', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
4

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

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
5 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', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
6 Thử nghiệmExperimental

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

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
7 theo các giá trị được trả về bởi hàm thử nghiệm.

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

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

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
3 hoặc
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
2 nếu thích hợp.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
01

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.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
02

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
03

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.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
04

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'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
7 nếu không tìm thấy.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
06

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.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
07

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
08

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'];

// 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 mới của mảng.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
10

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.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
11

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.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
12

Đả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.)

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
13

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
14

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
15

Trả về

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
3 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.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
17

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
18

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
19

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
20.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
21

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
22.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
23

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'];

// 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 mới của mảng.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
25

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.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
26

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
27 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'];
console.log(fruits.indexOf('Banana'));
// 1
1 và cuối cùng sử dụng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
29 để 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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
30 để tạo một chuỗi từ mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31.

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31 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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
33 để tìm vị trí (chỉ mục) của chuỗi

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
34 trong mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31.

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31 có chứa

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
34 và

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
38: Đầu tiên với phương thức
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
1, sau đó với phương thức

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
33 để kiểm tra giá trị chỉ mục không phải là
const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
7.

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
42 để nối một chuỗi mới vào mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31.

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
44 để xóa mục cuối cùng khỏi mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31.

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

Lưu ý:

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
44 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.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
44 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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
47 để loại bỏ 3 mục cuối cùng khỏi mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
0

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
47 để cắt giảm mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31 xuống chỉ còn 2 mục đầu tiên.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
1

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

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
51 để xóa mục đầu tiên khỏi mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
2

Lưu ý:

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
51 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.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
51 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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
47 để loại bỏ 3 mục đầu tiên khỏi mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
3

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
56 để thêm, tại Index
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, một mục mới cho mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31 - biến nó thành mục đầu tiên mới trong mảng.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
4

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
47 để xóa chuỗi

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
34 khỏi mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31 - bằng cách chỉ định vị trí chỉ mục của

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
34.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
5

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

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
47 để loại bỏ các chuỗi

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
34 và

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
65 khỏi mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31 - bằng cách chỉ định vị trí chỉ mục của

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
34, cùng với số lượng tổng số mục để xóa.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
6

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

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
47 để thay thế 2 mục cuối cùng trong mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31 bằng các mục mới.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
7

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

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
70 để lặp qua mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31, đăng nhập từng mục vào bảng điều khiển.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
8

Nhưng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
70 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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
73,

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
74,

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
75,

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
76,

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
77 và

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
78 - và xem ví dụ tiếp theo, sử dụng phương thức

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
79.

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
79 để gọi hàm trên mỗi phần tử trong mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31; 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.

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
9

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

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
82 để hợp nhất mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31 với mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
84, để tạo ra một mảng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
85 mới. Lưu ý rằng

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31 và

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
84 vẫn không thay đổi.

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
0

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
31 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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
89, sau đó bằng cách sử dụng phương thức

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
90.

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
1

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'];
console.log(fruits.indexOf('Banana'));
// 1
5,

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
14 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
6) 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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
94 để chuyển đổi mảng thành chuỗi JSON và sau đó

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
95 để 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.

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
2

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
96, 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:

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
3

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

Các phương thức

const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
5 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ó

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
98 và

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
99.

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
4

Để sử dụng

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
00, 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ề

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
99 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ử

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
99 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.

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
5

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', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
6. Điều này rất giống với

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
04 ngoại trừ việc nó nhóm các phần tử của mảng thành
const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop();
console.log(fruits);
// ["Apple", "Banana"]
console.log(removedItem);
// Orange
7 có thể sử dụng giá trị tùy ý (đối tượng hoặc nguyên thủy) làm 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

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
06 trong

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
07 lên

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
08. Vị trí cũ tại

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
09 được làm trống.

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
6

Đây là đầu ra:

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
7

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

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
8

Kết quả trong

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
9

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

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
10 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

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
11 và

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
12.

Ví dụ:

// '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
0

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

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
11 và

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
12.

Ghi 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
6 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

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
16 là một thuộc tính (tuy nhiên, cụ thể,

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
17 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ệ:

// '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
1

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ụ:

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
18 thay vì

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
19), mặc dù thường không cần thiết.

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
20 trong

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
19 được ép thành một chuỗi bởi công cụ JavaScript thông qua chuyển đổi

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
16 ngầm. Do đó,

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
23 và

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
24 sẽ đề cập đến hai vị trí khác nhau trên đối tượng

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
25 và ví dụ sau đây có thể là
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
3:

// '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
2

Chỉ

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
18 là một chỉ số mảng thực tế.

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
28 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'];

// 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 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ụ:

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
30,

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
90,

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
33, v.v.) có tính đến giá trị của thuộc tính
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 của mảng khi chúng được gọi.

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

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
42,

Copied!

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
47, v.v.) cũng dẫn đến các bản cập nhật cho thuộc tính
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 của mảng.

// '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
3

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'];

// 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 của mảng cho phù hợp:

// '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
4

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
9.

// '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
5

Giảm thuộc tính

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, tuy nhiên, xóa các yếu tố.

// '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
6

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

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
40.

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'];

// 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.

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

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
42 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'];

// 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:

  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    82
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    45
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    73
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    74
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    48
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    75
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    79
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    33
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    52
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    76
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    77
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    78
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    56
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    90
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    58
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    59
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    47

Để 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'];

// 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:

  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    62
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    63
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    64
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    65
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    66
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    67
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    00
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    69
  • const fruits = ['Apple', 'Banana'];
    const fruitsString = fruits.join(', ');
    console.log(fruitsString);
    // "Apple, Banana"
    
    1
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    30
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    72
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    73
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    27

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

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
75 để 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

    Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    76,

    Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    77 và

    Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    78 đố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

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
79:

  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    82
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    74
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    48
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    75
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    76
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    90
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    47 (để xây dựng mảng các phần tử bị loại bỏ đã được trả về)

Lưu ý rằng

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
00 và

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
69 không sử dụng

Copied!

console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
79 để 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'];

// 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 đơ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:

  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    45
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    63
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    44
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    42
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    56
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    51
  • Copied!

    console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
    59
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    47
  • Copied!

    const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // 👉️ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // 👉️ false
    56

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'];

// 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 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.

// '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
7

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

Thuộc tính

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 được chuyển đổi thành một số, bị cắt thành một số nguyên và sau đó được kẹp theo 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
3 trở thành
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, vì vậy ngay cả khi
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 không có mặt hoặc 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
2, nó hoạt động như thể nó 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
4 .

// '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
8

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

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 của đối tượng mảng. Họ luôn đặt giá trị sau khi chuẩn hóa, 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
9 luôn kết thúc dưới dạng số nguyên.

// '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
9

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'];

// 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 đượ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'];

// 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 và có các phần tử được lập chỉ mục trong phạm vi
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 đến
// '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
12. (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ụ,

// '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
13 và
// '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
14. Đối tượng
// '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
15 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'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
0

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