Pushing values into an associative array in JavaScript

This question already has an answer here:

  • How to append something to an array? 25 answers

  • 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});

    链接地址: http://www.djcxy.com/p/17942.html

    上一篇: json上的对象数组

    下一篇: 在JavaScript中将值推送到关联数组中