在JavaScript中将值推送到关联数组中

这个问题在这里已经有了答案:

  • 如何将某些东西附加到数组中? 25个答案

  • 你的代码的问题是你没有调用一个有效的对象。 请尝试下面的代码:

    <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/17941.html

    上一篇: Pushing values into an associative array in JavaScript

    下一篇: What’s a way to append a value to an array?