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
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.const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join[', '];
console.log[fruitsString];
// "Apple, Banana"
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ề
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.const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join[', '];
console.log[fruitsString];
// "Apple, Banana"
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
Trong JavaScript, các mảng không phải là nguyên thủy mà thay vào đó là Sự mô tả
6 đối tượng với các đặc điểm cốt lõi sau: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
- 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
4, phần thứ hai là tại Indexconst 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ínhconst 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 indexconst 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 indexconst 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'sconst 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 minusconst 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.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
- 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
1Tạ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
3Trả 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
5Tạ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
7Trả 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
0Tạ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
2Phả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
3Chứ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
5Trả 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
6Trả 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
7Sao 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
8Trả 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
9Trả 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
2Trả 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
4Trả 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
6Trả 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
8Trả 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
0Trả 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
2Trả 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
3Trả 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
4Gọ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ệmExperimentalNhó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ệmExperimentalNhó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
8Xá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.01Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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.
02Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Tham gia tất cả các yếu tố của một mảng thành một chuỗi.
03Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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.
04Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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.06Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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.
07Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Loại bỏ phần tử cuối cùng khỏi một mảng và trả về phần tử đó.
08Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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.10Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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.
11Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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.
12Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Đả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.]
13Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Loại bỏ phần tử đầu tiên khỏi một mảng và trả về phần tử đó.
14Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Trích xuất một phần của mảng gọi và trả về một mảng mới.
15Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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.17Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Sắp xếp các phần tử của một mảng tại chỗ và trả về mảng.
18Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Thêm và/hoặc loại bỏ các yếu tố khỏi một mảng.
19Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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
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
21Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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
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
23Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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.25Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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.
26Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Một bí danh cho phương thức
27 theo mặc định.Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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 29 để xây dựng mảng từ 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
// '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
30 để tạo một chuỗi từ mảngCopied!
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
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
31 bằng cách chỉ định số chỉ mục của vị trí của chú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
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
33 để tìm vị trí [chỉ mục] của chuỗiCopied!
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ảngCopied!
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
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
31 có chứaCopied!
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ứcCopied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join[', '];
console.log[fruitsString];
// "Apple, Banana"
1, sau đó với phương thức 33 để kiểm tra giá trị chỉ mục không phải làCopied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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
42 để nối một chuỗi mới vào mảngCopied!
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
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
44 để xóa mục cuối cùng khỏi mảngCopied!
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
const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop[];
console.log[fruits];
// ["Apple", "Banana"]
console.log[removedItem];
// Orange
Lưu ý: Copied!
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. 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.Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Xóa nhiều mục từ cuối một mảng
Ví dụ này sử dụng phương thức
47 để loại bỏ 3 mục cuối cùng khỏi mảngCopied!
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
0Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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
47 để cắt giảm mảngCopied!
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
1Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Xóa mục đầu tiên khỏi một mảng
Ví dụ này sử dụng phương thức
51 để xóa mục đầu tiên khỏi mảngCopied!
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
2Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Lưu ý: Copied!
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.
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.Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Xóa nhiều mục từ đầu một mảng
Ví dụ này sử dụng phương thức
47 để loại bỏ 3 mục đầu tiên khỏi mảngCopied!
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
3Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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
56 để thêm, tại IndexCopied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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 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
4Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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
47 để xóa chuỗiCopied!
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ảngCopied!
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ủaCopied!
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
5Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Xóa nhiều mục bằng chỉ mục
Ví dụ này sử dụng phương thức
47 để loại bỏ các chuỗiCopied!
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ảngCopied!
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ủaCopied!
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
6Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Thay thế nhiều mục trong một mảng
Ví dụ này sử dụng phương thức
47 để thay thế 2 mục cuối cùng trong mảngCopied!
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
7Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Lặp lại trên một mảng
Ví dụ này sử dụng vòng lặp
70 để lặp qua mảngCopied!
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
8Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Nhưng
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 choCopied!
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ứcCopied!
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
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
79 để gọi hàm trên mỗi phần tử trong mảngCopied!
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
9Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Hợp nhất nhiều mảng với nhau
Ví dụ này sử dụng phương thức
82 để hợp nhất mảngCopied!
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ảngCopied!
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ảngCopied!
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ằngCopied!
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!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
0Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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
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ứcCopied!
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ứcCopied!
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
1Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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, 14 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
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 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!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
2Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Bạn cũng có thể tạo các bản sao sâu bằng phương pháp
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.Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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:
3Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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ó
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!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
4Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Để sử dụng
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ử.Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Mã bên dưới sử dụng hàm mũi tên để trả về
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!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
5Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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 04 ngoại trừ việc nó nhóm các phần tử của mảng thànhCopied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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
06 trongCopied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
07 lênCopied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
08. Vị trí cũ tạiCopied!
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
6Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Đây là đầu ra:
7Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Sử dụng một mảng để lập bảng một tập hợp các giá trị
8Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Kết quả trong
9Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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
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ởiCopied!
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.Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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
11 vàCopied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
12.Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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
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ệ:Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
// '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
1Cú 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ụ:
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 trongCopied!
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 đổiCopied!
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ượngCopied!
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àCopied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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
2Chỉ
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.Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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ụ:
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ínhCopied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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ụ:
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ínhCopied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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
3Khi đặ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
4Tă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
5Giả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
40.Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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
42 trước khi truy cập chỉ mục và không kết hợp các khe trống vớiCopied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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:
82Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
45Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
73Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
74Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
48Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
75Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
79Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
33Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
52Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
76Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
77Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
78Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
56Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
90Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
58Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
59Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
47Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Để 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:
62Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
63Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
64Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
65Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
66Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
67Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
00Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
69Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
1const fruits = ['Apple', 'Banana']; const fruitsString = fruits.join[', ']; console.log[fruitsString]; // "Apple, Banana"
30Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
72Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
73Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
27Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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
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:Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
- Đố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
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.Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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
79:Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
82Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
74Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
48Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
75Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
76Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
90Copied!
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ề]Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Lưu ý rằng
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ụngCopied!
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ạoCopied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
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:
45Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
63Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
44Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
42Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
56Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
51Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
59Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
47Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
56Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
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
7Bì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
9Cá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"
0Thông số kỹ thuật
Đặ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