Làm cách nào để thêm giá trị vào một mảng trong javascript?

Phương thức đẩy () của JavaScript. Phương thức này sẽ thêm một phần tử vào cuối mảng, trong khi chức năng song sinh của nó, phương thức pop(), sẽ loại bỏ một phần tử khỏi phần cuối của mảng. Nếu bạn cần thêm một phần tử hoặc nhiều phần tử vào cuối một mảng, phương thức push() hầu như sẽ luôn là lựa chọn đơn giản và nhanh nhất của bạn.  

cú pháp

array.push(item1, item2, ..., itemX)

Thông số.  

  • mục1, mục2, …, mụcX. Đây là những thông số bắt buộc. (Các) mục sẽ được thêm vào cuối mảng.  

Ví dụ. Trong ví dụ này, chúng ta sẽ hợp nhất ba mảng lại với nhau bằng phương thức concat() của Javascript

Phương thức

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
3 thêm một hoặc nhiều phần tử vào cuối mảng và trả về độ dài mới của mảng

push(element0)
push(element0, element1)
push(element0, element1, /* … ,*/ elementN)

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
4

(Các) phần tử cần thêm vào cuối mảng

Thuộc tính

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
5 mới của đối tượng mà phương thức được gọi

Phương thức

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
3 nối các giá trị vào một mảng

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
7 có hành vi tương tự như
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
3, nhưng được áp dụng cho phần đầu của một mảng

Phương pháp

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
3 là một phương pháp đột biến. Nó thay đổi độ dài và nội dung của
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
2. Trong trường hợp bạn muốn giá trị của
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
2 giống nhau, nhưng trả về một mảng mới có các phần tử nối vào cuối, bạn có thể sử dụng
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
4 để thay thế. Lưu ý rằng các phần tử được bao bọc trong một mảng bổ sung — nếu không, nếu bản thân phần tử là một mảng, nó sẽ được trải ra thay vì được đẩy dưới dạng một phần tử do hành vi của
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
5

Phương pháp

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
3 là. Nó chỉ mong đợi giá trị
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
2 có thuộc tính
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
5 và các thuộc tính có khóa số nguyên. Mặc dù các chuỗi cũng giống như mảng, nhưng phương pháp này không phù hợp để áp dụng cho chúng, vì các chuỗi là bất biến

Đoạn mã sau tạo mảng

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
9 chứa hai phần tử, sau đó nối thêm hai phần tử vào nó. Biến
const vegetables = ["parsnip", "potato"];
const moreVegs = ["celery", "beetroot"];

// Merge the second array into the first one
vegetables.push(...moreVegs);

console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']
0 chứa độ dài mới của mảng

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4

Ví dụ này sử dụng cú pháp trải rộng để đẩy tất cả các phần tử từ mảng thứ hai vào mảng thứ nhất

________số 8

Hợp nhất hai mảng cũng có thể được thực hiện bằng phương thức

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
5

Phương thức

const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
3 đọc thuộc tính
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
5 của
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
2. Sau đó, nó đặt từng chỉ mục của
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
2 bắt đầu từ
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
5 với các đối số được chuyển đến
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
3. Cuối cùng, nó đặt
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
5 về độ dài trước đó cộng với số phần tử được đẩy

push(element0)
push(element0, element1)
push(element0, element1, /* … ,*/ elementN)
4

Như đã đề cập ở trên,

const vegetables = ["parsnip", "potato"];
const moreVegs = ["celery", "beetroot"];

// Merge the second array into the first one
vegetables.push(...moreVegs);

console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']
9 là chung chung có chủ ý và chúng tôi có thể sử dụng điều đó để tạo lợi thế cho mình.
push(element0)
push(element0, element1)
push(element0, element1, /* … ,*/ elementN)
40 có thể hoạt động tốt trên một đối tượng, như ví dụ này cho thấy

Lưu ý rằng chúng tôi không tạo một mảng để lưu trữ một bộ sưu tập các đối tượng. Thay vào đó, chúng tôi lưu trữ bộ sưu tập trên chính đối tượng và sử dụng

push(element0)
push(element0, element1)
push(element0, element1, /* … ,*/ elementN)
41 trên
push(element0)
push(element0, element1)
push(element0, element1, /* … ,*/ elementN)
40 để đánh lừa phương thức nghĩ rằng chúng tôi đang xử lý một mảng—và nó chỉ hoạt động, nhờ vào cách JavaScript cho phép chúng tôi thiết lập ngữ cảnh thực thi theo bất kỳ cách nào chúng tôi muốn

push(element0)
push(element0, element1)
push(element0, element1, /* … ,*/ elementN)
9

Lưu ý rằng mặc dù

push(element0)
push(element0, element1)
push(element0, element1, /* … ,*/ elementN)
43 không phải là một mảng, nhưng phương thức
const vegetables = ["parsnip", "potato"];
const moreVegs = ["celery", "beetroot"];

// Merge the second array into the first one
vegetables.push(...moreVegs);

console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']
9 đã tăng thành công thuộc tính
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
5 của
push(element0)
push(element0, element1)
push(element0, element1, /* … ,*/ elementN)
43 giống như khi chúng ta đang xử lý một mảng thực

Làm cách nào để thêm giá trị trong mảng JavaScript?

concat(Array, element); Khi bạn muốn thêm một phần tử vào cuối mảng, hãy sử dụng hàm push(). Nếu bạn cần thêm một phần tử vào đầu mảng, hãy sử dụng unshift(). Nếu bạn muốn thêm một phần tử vào một vị trí cụ thể của mảng, hãy sử dụng splice().

Làm cách nào để cộng các số trong một mảng JavaScript?

hàm sumArray(mảng) { const ourArray = [1, 4, 0, 9, -3]; . chiều dài; .
hàm sumArray(mảng) { cho tổng = 0; . forEach(mục => { tổng += mục;.
hàm sumArray(mảng) { cho tổng = 0;. */ for (const item của mảng) {