如何将对象存储到数组中,然后在create按钮元素中显示所有对象

how to store the object into array and then displays all the objects in the create button elements?

本文关键字:对象 按钮 create 元素 显示 存储 数组 然后      更新时间:2023-09-26

这是我迄今为止的代码:

var cars=["BMW","MERCEDEZ"]; 
var arr = [];
for (var j=0; j<= 1; j++){
    for (var x=0; x <= 5; x++) {
        arr ={
            index: a,
            name: arr[j] + (x+(5*j)),
            price: (x+(5*j))
        };
    }
}

我想在创建按钮元素中显示来自arr[]的所有对象:

btnShow = document.createElement("input");
btnShow.setAttribute("type", "button");

我该怎么做?

您需要向数组添加元素,而不是设置数组的不同对象instad

更换

arr = { 
          index: a, 
          name: cars[x+(5*j)],
          price: cars[x+(5*j)]
      };

带有

arr.push({ 
          index: a, 
          name: cars[x+(5*j)],
          price: cars[x+(5*j)]
      });