Add dynamic key, value pairs to JavaScript array or hash table

前端 未结 5 1625
醉话见心
醉话见心 2020-12-12 19:51

I\'m trying to add a key value pair to an existing javascript associative array. The key needs to be a variable. This is for JSON encoding. I realize there are many plugins

5条回答
  •  误落风尘
    2020-12-12 20:05

    var ary = [];
    
    function pushToAry(name, val) {
       var obj = {};
       obj[name] = val;
       ary.push(obj);
    }
    
    pushToAry("myName", "myVal");
    

    Having just fully read your question though, all you need is the following

    $(your collection of form els).serializeArray();
    

    Good old jQuery

提交回复
热议问题