简体   繁体   中英

Insert key in Json using Javascript

In the case of my json format as below

var  names = ['Lee' , 'Cooper'];

How can javascript programatically set the json output to below ?

{"suggestions": ['Lee', 'Cooper']}

I know to modify the value for names[0] from 'Lee' to something else. But I am not sure on how to set key. This is a pre-requisite for jquery autocomplete where the json format must be in the correct way.

var  names = ['Lee' , 'Cooper'];
{suggestions: names}
var  names = ['Lee' , 'Cooper'];
var arr = [];
arr.push({selection:names});
result = JSON.stringify(arr);

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