我有一个返回形式对象的函数:
[{"key":"name","value":"ali","key":"age","value":"56"}]当调用如下给出时.我怎么能让它返回相同类型的对象但没有方括号?
setProperties('{"name":"ali","age":"56"}');
function setProperties(str) {
var properties = [];
var json = jQuery.parseJSON(str);
for (property in json) {
properties.push({
key: property,
value: json[property]});
}
return properties;
}
Run Code Online (Sandbox Code Playgroud)
return properties[0]; // returns the first element of the list instead of the whole list
Run Code Online (Sandbox Code Playgroud)