简体   繁体   中英

Passing string as a property to object

function createObject(arg1, arg2){ 
       var object = {};
           object.arg1 = arg2;
}

I am calling my function like this.

   createObject('name', objectproperties);

The problem i am facing is when the request object is formed. Its something like this

{"arg1":{"Properties":"Value"}}

I want my object to be formed like this. Its a generic class, which i am creating for many requests to be formed.

{"name":{"Properties":"Value"}}

Use square bracket notation:

object[arg1] = arg2;

Reference: http://www.jibbering.com/faq/faq_notes/square_brackets.html#vId

object[arg1] = arg2;

这应该工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM