Làm cách nào để bạn thêm một phần tử vào một mảng trong HTML?

Mảng là danh sách của bất kỳ loại dữ liệu nào, kể cả các mảng khác. Mỗi mục trong mảng có một chỉ mục — một số — có thể được sử dụng để truy xuất một phần tử từ mảng

Show

    Các chỉ số bắt đầu từ 0;

    Trong JavaScript, bạn tạo một mảng bằng cú pháp mảng-chữ

    
    var emptyArray = [];
    
    var shoppingList = ['Milk', 'Bread', 'Beans'];
    

    Bạn truy xuất một phần tử cụ thể từ một mảng bằng cú pháp dấu ngoặc vuông

    
    shoppingList[0];
    
    Milk
    

    Cũng có thể đặt giá trị tại một chỉ mục cụ thể, một lần nữa bằng cách sử dụng cú pháp dấu ngoặc vuông

    
    shoppingList[1] = 'Cookies';
    
    // shoppingList is now ['Milk', 'Cookies', 'Beans']
    

    Làm cách nào để bạn thêm một phần tử vào một mảng trong HTML?
    Liên kết với chúng tôi. Nếu bạn thấy HTML Dog hữu ích, vui lòng cân nhắc việc liên kết với chúng tôi.

    Bạn có thể tìm số phần tử trong mảng bằng cách sử dụng thuộc tính

    
    shoppingList[0];
    
    2 của nó

    
    shoppingList.length;
    
    3
    

    Bạn có thể sử dụng các phương thức

    
    shoppingList[0];
    
    3 và
    
    shoppingList[0];
    
    4 để thêm và xóa các phần tử ở cuối mảng

    
    shoppingList.push('A new car');
    
    // shoppingList is now ['Milk', 'Bread', 'Beans', 'A new car']
    
    
    shoppingList.pop();
    
    
    shoppingList[0];
    
    0

    Đây là một ví dụ tạo, đẩy, bật và lặp trên một mảng, chuyển từng tên cho một hàm có tên là helloFrom. helloFrom trả về một chuỗi có lời chào. “Xin chào từ” và sau đó là tên của người đó. Sau khi đẩy và bật, danh sách những người cuối cùng là. “Tom”, “Yoda”, “Ron” và “Bob”

    Phương thức push() 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)
    

    elementN

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

    Thuộc tính length mới của đối tượng mà phương thức được gọi

    Phương thức push() nối các giá trị vào một mảng

    Array.prototype.unshift() có hành vi tương tự như push(), nhưng được áp dụng cho phần đầu của một mảng

    Phương thức push() là một phương thức độ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 với các phần tử được thêm 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 push() 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 length và 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

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

    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 push() đọc thuộc tính length 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ừ length với các đối số được chuyển đến push(). Cuối cùng, nó đặt length về độ dài trước đó cộng với số phần tử được đẩy

    const arrayLike = {
      length: 3,
      unrelated: "foo",
      2: 4,
    };
    Array.prototype.push.call(arrayLike, 1, 2);
    console.log(arrayLike);
    // { '2': 4, '3': 1, '4': 2, length: 5, unrelated: 'foo' }
    
    const plainObj = {};
    // There's no length property, so the length is 0
    Array.prototype.push.call(plainObj, 1, 2);
    console.log(plainObj);
    // { '0': 1, '1': 2, length: 2 }
    

    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.
    const arrayLike = {
      length: 3,
      unrelated: "foo",
      2: 4,
    };
    Array.prototype.push.call(arrayLike, 1, 2);
    console.log(arrayLike);
    // { '2': 4, '3': 1, '4': 2, length: 5, unrelated: 'foo' }
    
    const plainObj = {};
    // There's no length property, so the length is 0
    Array.prototype.push.call(plainObj, 1, 2);
    console.log(plainObj);
    // { '0': 1, '1': 2, length: 2 }
    
    0 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

    const arrayLike = {
      length: 3,
      unrelated: "foo",
      2: 4,
    };
    Array.prototype.push.call(arrayLike, 1, 2);
    console.log(arrayLike);
    // { '2': 4, '3': 1, '4': 2, length: 5, unrelated: 'foo' }
    
    const plainObj = {};
    // There's no length property, so the length is 0
    Array.prototype.push.call(plainObj, 1, 2);
    console.log(plainObj);
    // { '0': 1, '1': 2, length: 2 }
    
    1 trên
    const arrayLike = {
      length: 3,
      unrelated: "foo",
      2: 4,
    };
    Array.prototype.push.call(arrayLike, 1, 2);
    console.log(arrayLike);
    // { '2': 4, '3': 1, '4': 2, length: 5, unrelated: 'foo' }
    
    const plainObj = {};
    // There's no length property, so the length is 0
    Array.prototype.push.call(plainObj, 1, 2);
    console.log(plainObj);
    // { '0': 1, '1': 2, length: 2 }
    
    0 để 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

    const obj = {
      length: 0,
    
      addElem(elem) {
        // obj.length is automatically incremented
        // every time an element is added.
        [].push.call(this, elem);
      },
    };
    
    // Let's add some empty objects just to illustrate.
    obj.addElem({});
    obj.addElem({});
    console.log(obj.length); // 2
    

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

    const arrayLike = {
      length: 3,
      unrelated: "foo",
      2: 4,
    };
    Array.prototype.push.call(arrayLike, 1, 2);
    console.log(arrayLike);
    // { '2': 4, '3': 1, '4': 2, length: 5, unrelated: 'foo' }
    
    const plainObj = {};
    // There's no length property, so the length is 0
    Array.prototype.push.call(plainObj, 1, 2);
    console.log(plainObj);
    // { '0': 1, '1': 2, length: 2 }
    
    3 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 length của
    const arrayLike = {
      length: 3,
      unrelated: "foo",
      2: 4,
    };
    Array.prototype.push.call(arrayLike, 1, 2);
    console.log(arrayLike);
    // { '2': 4, '3': 1, '4': 2, length: 5, unrelated: 'foo' }
    
    const plainObj = {};
    // There's no length property, so the length is 0
    Array.prototype.push.call(plainObj, 1, 2);
    console.log(plainObj);
    // { '0': 1, '1': 2, length: 2 }
    
    3 giống như khi chúng ta đang xử lý một mảng thực