在JavaScript中将值推送到关联数组中
这个问题在这里已经有了答案:
你的代码的问题是你没有调用一个有效的对象。 请尝试下面的代码:
<script>
var car = [];
car [0] = {type: "audi", age: "10"};
car [1] = {type: "bmw", age: "6"};
car [2] = {type: "tesla", age: "2"};
console.log(car);
value1 = "hyundai"
value2 = "14";
car.push({type: value1, age: value2});
console.log(car);
</script>
你可以car.push({type: value1, age: value2});