Hướng dẫn function that takes in an array javascript - hàm nhận trong một mảng javascript

Tôi có một mảng và tôi muốn truyền nó dưới dạng tham số trong một hàm như:

function something[arrayP]{
    for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i].value];
    }
 }

Tôi đang nhận được mảng đó [0] không được xác định, điều này có thể đúng vì bên trong hàm tôi chưa bao giờ viết loại mảng mảng nào. Vì thế,

  1. Có thể vượt qua mảng dưới dạng tham số không?
  2. Nếu vậy, những yêu cầu bên trong chức năng là gì?

Hỏi ngày 18 tháng 12 năm 2010 lúc 19:04Dec 18, 2010 at 19:04

1

Chỉ cần xóa

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

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

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

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

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
0, như thế này:

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}

Chắc chắn bạn có thể vượt qua một mảng, nhưng để có được phần tử ở vị trí đó, chỉ sử dụng

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

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

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

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

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
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
0 sẽ lấy 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
3 khỏi một đối tượng ở vị trí đó trong mảng - đối với những thứ như chuỗi, số, vv không hiện hữu. Ví dụ,
const fruits = ['Apple', 'Banana'];

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

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

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

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
4 cũng sẽ 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
5.

Đã trả lời ngày 18 tháng 12 năm 2010 lúc 19:07Dec 18, 2010 at 19:07

Nick Cravernick CraverNick Craver

616K134 Huy hiệu vàng1293 Huy hiệu bạc1152 Huy hiệu đồng134 gold badges1293 silver badges1152 bronze badges

2

JavaScript là một ngôn ngữ được đánh máy động. Điều này có nghĩa là bạn không bao giờ cần phải khai báo loại đối số hàm [hoặc bất kỳ biến nào khác]. Vì vậy, mã của bạn sẽ hoạt động miễn 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 là một mảng và chứa các phần tử 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
3.

Đã trả lời ngày 18 tháng 12 năm 2010 lúc 19:09Dec 18, 2010 at 19:09

AmnonamnonAmnon

7.5122 Huy hiệu vàng25 Huy hiệu bạc34 Huy hiệu đồng2 gold badges25 silver badges34 bronze badges

Có thể chuyển các mảng cho các chức năng và không có yêu cầu đặc biệt để xử lý chúng. Bạn có chắc chắn rằng mảng bạn đang chuyển đến chức năng của bạn thực sự có một phần tử tạ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
8?

Đã trả lời ngày 18 tháng 12 năm 2010 lúc 19:07Dec 18, 2010 at 19:07

Nick Cravernick CraverClosureCowboy

616K134 Huy hiệu vàng1293 Huy hiệu bạc1152 Huy hiệu đồng13 gold badges56 silver badges70 bronze badges

Sự mô tả

Trong JavaScript, các mảng không phải là nguyên thủy mà thay vào đó là các đối tượng ____49 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à tại Index
    const fruits = ['Apple', 'Banana'];
    console.log[fruits.indexOf['Banana']];
    // 1
    
    0, phần thứ hai là tại Index
    const fruits = ['Apple', 'Banana'];
    console.log[fruits.indexOf['Banana']];
    // 1
    
    1, v.v.-và phần tử cuối cùng là giá trị của thuộc tính
    const fruits = ['Apple', 'Banana'];
    console.log[fruits.indexOf['Banana']];
    // 1
    
    2 của mảng trừ
    const fruits = ['Apple', 'Banana'];
    console.log[fruits.indexOf['Banana']];
    // 1
    
    1.
    : the first element of an array is at index
    const fruits = ['Apple', 'Banana'];
    console.log[fruits.indexOf['Banana']];
    // 1
    
    0, the second is at index
    const fruits = ['Apple', 'Banana'];
    console.log[fruits.indexOf['Banana']];
    // 1
    
    1, and so on — and the last element is at the value of the array's
    const fruits = ['Apple', 'Banana'];
    console.log[fruits.indexOf['Banana']];
    // 1
    
    2 property minus
    const fruits = ['Apple', 'Banana'];
    console.log[fruits.indexOf['Banana']];
    // 1
    
    1.
  • 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
4

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
9 mới.

Tính chất tĩnh

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

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

Phương pháp tĩnh

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

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
9 mới từ một đối tượng giống như mảng hoặc đối tượng có thể lặp lại.

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

fruits.includes['Banana']; // true
fruits.includes['Cherry']; // false

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

Trả về

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

fruits.includes['Banana']; // true
fruits.includes['Cherry']; // false

// If indexOf[] doesn't return -1, the array contains the given item.
fruits.indexOf['Banana'] !== -1; // true
fruits.indexOf['Cherry'] !== -1; // false
1 nếu đối số là một mảng hoặ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
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
3

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
9 mới với một số lượng đối số thay đổi, bất kể số hoặc loại đối số.

Thuộc tính thể hiện

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

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

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
6

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

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
8

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
9

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

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

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

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

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

Trả về

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

fruits.includes['Banana']; // true
fruits.includes['Cherry']; // false

// If indexOf[] doesn't return -1, the array contains the given item.
fruits.indexOf['Banana'] !== -1; // true
fruits.indexOf['Cherry'] !== -1; // false
1 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
4

Đ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
5

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

fruits.includes['Banana']; // true
fruits.includes['Cherry']; // false

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

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

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

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

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

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
5 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
3

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

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

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
6

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
7

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
8 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
9 Thử nghiệmExperimental

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

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

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

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

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

fruits.includes['Banana']; // true
fruits.includes['Cherry']; // false

// If indexOf[] doesn't return -1, the array contains the given item.
fruits.indexOf['Banana'] !== -1; // true
fruits.indexOf['Cherry'] !== -1; // false
1 hay
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 nếu thích hợp.

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

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

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

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

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

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

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

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

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

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

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.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
00

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
01

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'];
console.log[fruits.indexOf['Banana']];
// 1
2 mới của mảng.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
03

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.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
04

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.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
05

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
06

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
07

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
08

Trả về

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

fruits.includes['Banana']; // true
fruits.includes['Cherry']; // false

// If indexOf[] doesn't return -1, the array contains the given item.
fruits.indexOf['Banana'] !== -1; // true
fruits.indexOf['Cherry'] !== -1; // false
1 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.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
10

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
11

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
12

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
13.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
14

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
15.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
16

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'];
console.log[fruits.indexOf['Banana']];
// 1
2 mới của mảng.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
18

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.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
19

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
20 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
4 và cuối cùng sử dụng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
22 để 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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
23 để tạo một chuỗi từ mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24.

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24 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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
26 để tìm vị trí [chỉ mục] của chuỗi
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
27 trong mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24.

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24 có chứa
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
27 và
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
31: Đầu tiên với phương thức
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
32, sau đó với phương thức
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
26 để kiểm tra giá trị chỉ mục không phải là
const fruits = ['Apple', 'Banana', 'Orange'];
const removedItem = fruits.pop[];
console.log[fruits];
// ["Apple", "Banana"]
console.log[removedItem];
// Orange
0.

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
35 để nối một chuỗi mới vào mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24.

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
37 để xóa mục cuối cùng khỏi mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24.

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

Lưu ý:

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
37 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.
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
37 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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
40 để loại bỏ 3 mục cuối cùng khỏi mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24.

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

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

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
40 để cắt giảm mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24 xuống chỉ còn 2 mục đầu tiên.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
0

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

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
44 để xóa mục đầu tiên khỏi mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
1

Lưu ý:

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
44 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.
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
44 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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
40 để loại bỏ 3 mục đầu tiên khỏi mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
2

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
49 để thêm, tại Index
const fruits = ['Apple', 'Banana'];
console.log[fruits.indexOf['Banana']];
// 1
0, một mục mới cho mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24 - biến nó thành mục đầu tiên mới trong mảng.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
3

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
40 để xóa chuỗi
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
27 khỏi mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24 - bằng cách chỉ định vị trí chỉ mục của
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
27.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
4

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

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
40 để loại bỏ các chuỗi
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
27 và
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
58 khỏi mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24 - bằng cách chỉ định vị trí chỉ mục của
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
27, cùng với số lượng tổng số mục để xóa.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
5

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

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
40 để thay thế 2 mục cuối cùng trong mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24 bằng các mục mới.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
6

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

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
63 để lặp qua mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24, đăng nhập từng mục vào bảng điều khiển.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
7

Nhưng

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
63 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
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
66,
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
67,
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
68,
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
69,
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
70 và
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
71 - và xem ví dụ tiếp theo, sử dụng phương pháp
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
72.

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
72 để gọi hàm trên mỗi phần tử trong mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24; 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.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
8

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

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
75 để hợp nhất mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24 với mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
77, để tạo ra một mảng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
78 mới. Lưu ý rằng
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24 và
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
77 vẫn không thay đổi.

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
9

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
24 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
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
82, sau đó bằng cách sử dụng phương thức
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
83.

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

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
8,
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
07 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
9] 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
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
87 để chuyển đổi mảng thành chuỗi JSON và sau đó
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
88 để 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.

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

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
89, 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:

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

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
8 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ó

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
91 và
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
92.

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

Để sử dụng

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
93, 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ề

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
92 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ử
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
92 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.

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

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
9. Điều này rất giống với
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
97 ngoại trừ việc nó nhóm các phần tử của mảng thành
const fruits = ['Apple', 'Banana', 'Strawberry', 'Mango', 'Cherry'];
const start = -3;
const removedItems = fruits.splice[start];
console.log[fruits];
// ["Apple", "Banana"]
console.log[removedItems];
// ["Strawberry", "Mango", "Cherry"]
0 có thể sử dụng giá trị tùy ý [đối tượng hoặc nguyên thủy] làm chìa khóa.

Những ví dụ khác

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

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
99 trong
// '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
00 sang
// '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
01. Vị trí cũ tạ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
02 được làm trố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
5

Đây là đầu ra:

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

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

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

Kết quả trong

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

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

// '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
03 và một chuỗi có thể tạo ra một mảng JavaScript có các thuộc tính và phần tử cung cấp thông tin về trận đấu. Một mảng như vậy được trả lại bở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
04 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
05.

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
9

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

// '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
04 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
05.

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
9 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 theo cùng một cách mà

// '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
09 là một thuộc tính [tuy nhiên, cụ thể,
// '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
10 là một phương thức]. Tuy nhiên, cố gắng truy cập một phần tử của một mảng như sau đã ném lỗi cú pháp vì tên thuộc tính không hợp lệ:

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

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

// '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
11 thay 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
12], mặc dù thường không cần thiế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
13 trong
// '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 được ép thành một chuỗi bởi công cụ JavaScript thông qua chuyển đổ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
09 ngầm. Do đó,
// '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
16 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
17 sẽ đề cập đến hai vị trí khác nhau trên đố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
18 và ví dụ sau đây có thể là
const fruits = ['Apple', 'Banana'];

fruits.includes['Banana']; // true
fruits.includes['Cherry']; // false

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

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

Chỉ

// '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
11 là một chỉ số mảng thực 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
21 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'];
console.log[fruits.indexOf['Banana']];
// 1
2 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ụ:

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
23,
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
83,
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
26, v.v.] có tính đến giá trị của thuộc tính
const fruits = ['Apple', 'Banana'];
console.log[fruits.indexOf['Banana']];
// 1
2 của mảng khi chúng được gọi.

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

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
35,
function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
40, v.v.] cũng dẫn đến các bản cập nhật cho thuộc tính
const fruits = ['Apple', 'Banana'];
console.log[fruits.indexOf['Banana']];
// 1
2 của mảng.

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

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'];
console.log[fruits.indexOf['Banana']];
// 1
2 của mảng cho phù hợp:

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

Tăng

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

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

Giảm thuộc tính

const fruits = ['Apple', 'Banana'];
console.log[fruits.indexOf['Banana']];
// 1
2, tuy nhiên, xóa các yếu tố.

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

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

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

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

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

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

  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    75
  • // '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
    
    38
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    66
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    67
  • // '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
    
    41
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    68
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    72
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    26
  • // '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
    
    45
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    69
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    70
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    71
  • // '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
    
    49
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    83
  • // '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
    
    51
  • // '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
    
    52
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    40

Để 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
5:

  • // '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
    
    55
  • // '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
    
    56
  • // '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
    
    57
  • // '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
    
    58
  • // '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
    
    59
  • // '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
    
    60
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    93
  • // '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
    
    62
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    32
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    23
  • // '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
    
    65
  • // '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
    
    66
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    20

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

// '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
68 để 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
    // '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
    
    69,
    // '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
    
    70 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
    
    71 đố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

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

  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    75
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    67
  • // '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
    
    41
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    68
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    69
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    83
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    40 [để xây dựng mảng các phần tử bị loại bỏ đã được trả về]

Lưu ý rằng

function[arrayP]{    
   for[var i = 0; i < arrayP.length; i++]{
      alert[arrayP[i]];    //no .value here
   }
}
93 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
62 không sử dụ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
72 để 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
9 trơ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:

  • // '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
    
    38
  • // '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
    
    56
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    37
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    35
  • // '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
    
    49
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    44
  • // '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
    
    52
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    40
  • function[arrayP]{    
       for[var i = 0; i < arrayP.length; i++]{
          alert[arrayP[i]];    //no .value here
       }
    }
    
    49

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'];
console.log[fruits.indexOf['Banana']];
// 1
2 và các phần tử được lập chỉ mục. Điều này có nghĩa là chúng cũng có thể được gọi trên các đối tượng giống như mảng.

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

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

Thuộc tính

const fruits = ['Apple', 'Banana'];
console.log[fruits.indexOf['Banana']];
// 1
2 đượ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.
// '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
95 trở thành
const fruits = ['Apple', 'Banana'];
console.log[fruits.indexOf['Banana']];
// 1
0, vì vậy ngay cả khi
const fruits = ['Apple', 'Banana'];
console.log[fruits.indexOf['Banana']];
// 1
2 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
5, nó hoạt động như thể nó có giá trị ____5050 .

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

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

const fruits = ['Apple', 'Banana'];
console.log[fruits.indexOf['Banana']];
// 1
2 của đối tượng mảng. Họ luôn đặt giá trị sau khi chuẩn hóa, do đó
const fruits = ['Apple', 'Banana'];
console.log[fruits.indexOf['Banana']];
// 1
2 luôn kết thúc như một số nguyên.

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

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'];
console.log[fruits.indexOf['Banana']];
// 1
2 đượ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'];
console.log[fruits.indexOf['Banana']];
// 1
2 và có các phần tử được lập chỉ mục trong phạm vi
const fruits = ['Apple', 'Banana'];
console.log[fruits.indexOf['Banana']];
// 1
0 đến
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join[', '];
console.log[fruitsString];
// "Apple, Banana"
05. [Nếu nó không có tất cả các chỉ số, nó sẽ tương đương về mặt chức năng với một mảng thưa thớt.]

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

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join[', '];
console.log[fruitsString];
// "Apple, Banana"
06 và
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join[', '];
console.log[fruitsString];
// "Apple, Banana"
07. Đối tượng
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join[', '];
console.log[fruitsString];
// "Apple, Banana"
08 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"
9

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

Bạn có thể đặt một chức năng bên trong một mảng javascript không?

Câu trả lời đơn giản là có bạn có thể đặt chức năng trong một mảng. Trong thực tế, có thể khai báo các biến và tham chiếu chúng trong chức năng của bạn.yes you can place function in an array. In fact, can declare variables and reference them in your function.

Bạn có thể gọi một chức năng trong một mảng không?

Điều quan trọng cần nhớ là khi một mảng được sử dụng làm đối số hàm, địa chỉ của nó được chuyển đến một hàm.Điều này có nghĩa là mã bên trong hàm sẽ hoạt động và có khả năng thay đổi nội dung thực tế của mảng được sử dụng để gọi hàm.when an array is used as a function argument, its address is passed to a function. This means that the code inside the function will be operating on, and potentially altering, the actual content of the array used to call the function.

Làm thế nào để bạn cung cấp cho một mảng một chức năng trong JavaScript?

Phương pháp 1: Sử dụng phương thức application []: Phương thức application [] được sử dụng để gọi hàm với các đối số đã cho là một mảng hoặc đối tượng giống như mảng.Nó chứa hai tham số.Giá trị này cung cấp một cuộc gọi đến hàm và mảng đối số chứa mảng các đối số sẽ được truyền.Using the apply[] method: The apply[] method is used to call a function with the given arguments as an array or array-like object. It contains two parameters. The this value provides a call to the function and the arguments array contains the array of arguments to be passed.

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

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

Bài Viết Liên Quan

Chủ Đề