Push an associative item into an array in JavaScript

前端 未结 4 695
死守一世寂寞
死守一世寂寞 2020-12-13 08:26

How can I correct the following code?

4条回答
  •  既然无缘
    2020-12-13 08:50

    To make something like associative array in JavaScript you have to use objects. ​

    var obj = {}; // {} will create an object
    var name = "name";
    var val = 2;
    obj[name] = val;
    console.log(obj);

    DEMO: http://jsfiddle.net/bz8pK/1/

提交回复
热议问题