[英]Add multiple key value pairs in a JavaScript object
I want a JavaScript object which should have multiple key values pairs.我想要一个应该有多个键值对的 JavaScript 对象。 However, I am unable to add.但是,我无法添加。 On clicking a function, I am getting multiple key value pairs as单击一个函数时,我得到多个键值对
{id:"1" ,desc: "first"} , {id:"2", desc:"two"} , {id:"3" , desc:"three"} ...
How can I add them to the object so that the final object should look like this:我怎样才能将它们添加到对象中,以便最终对象看起来像这样:
prdObj= ({id:"1" ,desc: "first"} , {id:"2", desc:"two"} , {id:"3" , desc:"three"});
Try this:试试这个:
prdObj = [{id:"1" ,desc: "first"} , {id:"2", desc:"two"} , {id:"3" , desc:"three"}];
//when adding
prdObj.push( {id:"4" , desc:"four"});
You can try this with single key value:您可以尝试使用单个键值:
var yourObject = [{"yourkey":"yourvalue"}, {"yourkey":"yourvalue"}];
OR要么
var yourObject = [{"key1":"yourvalue", "key2":"yourvalue"}, {"key1":"yourvalue", "key2":"yourvalue"}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.