[英]JSON to array of objects, and adding item to each object
I am trying to retrieve data from a JSON file and creating an array of objects (works fine), but I also want to add an item to each object in the array while doing so. 我正在尝试从JSON文件检索数据并创建对象数组(工作正常),但同时我还想向数组中的每个对象添加一个项目。
This is my code: 这是我的代码:
var linklist = [];
$.getJSON('links.json').success(function (json) {
$.each(json, function(i, obj) {
linklist.push(obj);
$(obj).extend({
status:'Downloaded'
});
});
})
If I try to output data in status, I get 'undefined'
as a value. 如果我尝试以状态输出数据,则会得到'undefined'
作为值。
Do this. 做这个。
var linklist = [];
$.getJSON('links.json').success(function (json) {
$.each(json, function(i, obj) {
// add status property to current object
obj.status = 'Downloaded';
// add updated object to array
linklist.push(obj)
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.