Pushing values into an associative array in JavaScript
This question already has an answer here:
The issue with your code is that you were not calling push on a valid object. Please try this code:
<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});
上一篇: json上的对象数组