我面临一个小问题的问题。我想使用JavaScript for循环以下面的格式创建一个JSON数组。
var mainJson = {
"people": [{
"name": "Ravi",
"role": "team member",
"appointments": [{
"info": "room 1",
"type": "Type01"
"title": "Meet John Miller"
}, {
"info": "room 2",
"type": "Type02"
"title": "Meet Mitchell"
}]
]
}
};
要创建以上JSON,我有2个jsons,如下所示..
var subJson1 = [{
"RowId": "00272727",
"Rowlabel": "Ravi",
"role": "team member"
}]
var subJson2 = [{
"info": "room 1",
"RowId": "00272727",
"type": "Type01",
"title": "Meet John Miller"
}, {
"info": "room 2",
"type": "Type02",
"RowId": "00272727",
"title": "Meet Mitchell"
}]
此处同时包含 SubJson1 和 subJson2 " RowID "是常见的参数。基于 RowId ,我们必须创建 mainJson 。在人员中的 mainJson 数组对象将超过1。我只是只显示一个项目以供理解。
我试图通过for循环来实现这一点。但我面临着解决这个问题的问题。有人可以帮助我使用 subJson1 和 subJson2
创建 mainJson提前谢谢。
答案 0 :(得分:0)
子JSON值是否总是以相同的格式出现?如果是这样,您可以跳过for
循环的麻烦,只需通过函数传递值。
编辑:请参阅下面的编辑答案。
const subJson1 = [
{
"RowId": "00272727",
"Rowlabel": "Ravi",
"role": "team member"
}
]
const subJson2 = [
{
"info": "room 1",
"RowId": "00272727",
"type": "Type01",
"title": "Meet John Miller"
},
{
"info": "room 2",
"type": "Type02",
"RowId": "00272727",
"title": "Meet Mitchell"
}
]
const consolidateJSON = (subJson1, subJson2) => {
return {
people: [
{
name: subJson1[0].Rowlabel,
role: subJson1[0].role,
appointments: subJson2.map( item => {
const {info, type, title} = item;
return {
info,
type,
title
}
} )
}
]
}
}
答案 1 :(得分:0)
以下是使用ES6 select [ZRDocs].*
from [ZRDocs] left join ([ZRDocItems] IN "D:\2222.mdb") AS abc
on [ZRDocs].[ID] = [abc].[DocID];
按RowId
映射约会人员的方法:
const subJson1 = [{
"RowId": "00272727",
"Rowlabel": "Ravi",
"role": "team member"
}]
const subJson2 = [{
"info": "room 1",
"RowId": "00272727",
"type": "Type01",
"title": "Meet John Miller"
}, {
"info": "room 2",
"type": "Type02",
"RowId": "00272727",
"title": "Meet Mitchell"
}]
/**
* Get people, mapped with their appointments
*
* @people {Object[]}
* @appointments {Object[]}
*/
const getPeopleWithAppointments = (people, appointments) => ({
people: people.map(person => ({
...person,
appointments: appointments.filter(({ RowId }) => RowId === person.RowId)
}))
})
console.log(getPeopleWithAppointments(subJson1, subJson2))
答案 2 :(得分:0)
试试这个:
var subJson1 = [{
"RowId": "00272727",
"Rowlabel": "Ravi",
"role": "team member"
}];
var subJson2 = [{
"info": "room 1",
"RowId": "00272727",
"type": "Type01",
"title": "Meet John Miller"
}, {
"info": "room 2",
"type": "Type02",
"RowId": "00272727",
"title": "Meet Mitchell"
}];
var mainJson = {"people" : []};
mainJson.people.push({"appointments" : subJson2.map(item => {
delete item.RowId;
return item;
})
});
mainJson.people[0].name = subJson1[0].Rowlabel;
mainJson.people[0].role = subJson1[0].role;
console.log(mainJson);

答案 3 :(得分:-1)
如果您只是尝试合并两个对象,那么有许多实用程序会执行此操作,我会遵循这些实用程序。因此this等问题也提供了几种方法。
但是,你似乎并不是在寻找这个。在您的情况下,您似乎想要创建一个具有新结构的新对象,并将其他两个对象插入其中。为此,您不会使用循环,而只需执行以下操作:var subJson1 = {
"RowId": "00272727",
"Rowlabel": "Ravi",
"role": "team member"
}
var subJson2 = [
{
"info": "room 1",
"type": "Type01"
"title": "Meet John Miller"
},
{
"info": "room 2",
"type": "Type02"
"title": "Meet Mitchell"
}
];
var mainJson = {
"people": [{
"name": subJson1.Rowlabel,
"role": subJson1.role,
"appointments": subJson2
}]
};
请记住;你不需要在Javascript(.js文件)中放置你的密钥,只能在JSON(.json文件)中。