JSON与Javascript的转换
我有一个json文件,如下所示 [{ "id": 2, "name": "Ali", "records":[{ "type": "L", "total": 123 }, { "type": "P", "total": 102 }] },{ "id": 3, "name": "Mete", "records":[{ "type": "O", "total": 100 }, { "type": "T", "total": 88 }] }] [{ “id”:2, “姓名”:“阿里”, “记录”:[{ “类型”:“L”, “总数”:123 }, { “类型”:“P”, “总数”:102 }] },{ “id”:3, “名称”:“米”, “记录”:[{ “类型”:“O”, “总数”:100 }, { “类型”:“T”, “总数”:88 }] }] 我想变成这样 [{ "id": 2, "name": "Ali", record: { "type": "L", "total": 123 } },{ "id": 2, "name": "Ali", record: { "type": "P", "total": 102 } },{ "id": 3, "name": "Mete", record: { "type": "O", "total": 100 } },{ "id": 3, "name": "Mete", record: { "type": "T", "total": 88 } }] [{ “id”:2, “姓名”:“阿里”, 记录:{ “类型”:“L”, “总数”:123 } },{ “id”:2, “姓名”:“阿里”, 记录:{ “类型”:“P”, “总数”:102 } },{ “id”:3, “名称”:“米”, 记录:{ “类型”:“O”, “总数”:100 } },{ “id”:3, “名称”:“米”, 记录:{ “类型”:“T”, “总数”:88 } }]JSON与Javascript的转换,javascript,json,Javascript,Json,我有一个json文件,如下所示 [{ "id": 2, "name": "Ali", "records":[{ "type": "L", "total": 123 }, { "type": "P", "total": 102 }] },{ "id": 3, "name": "Mete", "records":[{ "type": "O",
如何使用javascript实现这一点?有几种方法,但Array.prototype.map似乎很适合。这篇导语应该让你开始:
// We'll assume your original array is called myArray
var newArray = myArray.map ( function ( d ) {
return {
id : (d.id || 'theIDYouWant'),
name : (d.name || 'theNameYouWant' ),
record : { type : d.type, total : d.total }
}
} );
var jsonStr = JSON.stringify ( newArray );
console.log ( jsonStr ); // should write out your JSON as expected.
以下是一种实用(但可能不是很有效)的方法:
function transform(data) {
// Merge the sub arrays together
return [].concat.apply([], (data.map(person => {
// Return an array of copied objects for each person
return person.records.map(record => {
// For each record, copy the person information
return {
id: person.id,
name: person.name,
record: record
};
});
})));
}
console.log(transform([{
"id": 2,
"name": "Ali",
"records":[{
"type": "L",
"total": 123
}, {
"type": "P",
"total": 102
}]
},{
"id": 3,
"name": "Mete",
"records":[{
"type": "O",
"total": 100
}, {
"type": "T",
"total": 88
}]
}]));
这是你能做的。但是,这在IE中不起作用,因为正在使用的Object.assign在IE中还不受支持。但是,它可以被任何javascript对象克隆方法所取代 您可以检查:
var输入=[{
“id”:2,
“姓名”:“阿里”,
“记录”:[{
“类型”:“L”,
“总数”:123
}, {
“类型”:“P”,
“总数”:102
}]
}, {
“id”:3,
“名称”:“米”,
“记录”:[{
“类型”:“O”,
“总数”:100
}, {
“类型”:“T”,
“总数”:88
}]
}];
var输出=[];
input.forEach((obj)=>{
var记录=对象记录;
删除目标记录;
记录。forEach((记录)=>{
//在IE中没有任何支持。
var newRecord=Object.assign({},obj);
newRecord.record=记录;
输出推送(newRecord);
});
});
控制台日志(输出)代码>这将完成并在new\u json
- 尝试运行下面的代码段
json=[{
“id”:2,
“姓名”:“阿里”,
“记录”:[{
“类型”:“L”,
“总数”:123
}, {
“类型”:“P”,
“总数”:102
}]
}, {
“id”:3,
“名称”:“米”,
“记录”:[{
“类型”:“O”,
“总数”:100
}, {
“类型”:“T”,
“总数”:88
}]
}]
新的json=[];
for(var i=0;i log(新的_-json)代码>这并不能真正回答问题,在初始数据上使用map意味着您将只获得包含的第一个“记录”项。开始OP有点困难。我相信他会发现他也可以在嵌套循环中迭代记录对象。当然,如果您愿意,您可以写出所有代码,但我认为“使用Array.prototype.map”是一个有用的答案。谢谢@Sreekanth。它是用铬做的。请告诉我,除了IE,我还可以怎么做。正如我在回答中提到的,你必须自己实现一个克隆方法(看看我发布的链接)。或者您可以使用jQuery.extend()进行研究,然后编写注释,我从angular copy方法中找到它。非常感谢你。对于经典方法,我也做不到。因为我认为数组推送方法是异步的