[英]javascript nested Json object from flat object
我如何从平面 object 创建嵌套的 Json object。如果不同对象的 hod 和 dep 代码相同,则添加相同的嵌套 object。//////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// ///////////////////////////////我的公寓 object ==>>
flatObj = [
{
hod : '1000',
dep : '2',
teacher : 'avi',
teacherno : '121',
teacheradd : 'mumbai',
teacheraddno : '133',
billtoname : 'manisha',
billtono : '77',
payname : 'mann',
payno : '99'
},
{
hod : '1567',
dep : '2',
teacher : 'shetty',
teacherno : '166',
teacheradd : 'gujrat',
teacheraddno : '190',
billtoname : 'annu',
billtono : '87',
payname : 'kiru',
payno : '495'
},
{
hod : '1567',
dep : '2',
teacher : 'shetty',
teacherno : '166',
teacheradd : 'gujrat',
teacheraddno : '190',
billtoname : 'raina',
billtono : '03',
payname : 'kiru',
payno : '495'
},
{
hod : '1000',
dep : '2',
teacher : 'kisha',
teacherno : '654',
teacheradd : 'pune',
teacheraddno : '986',
billtoname : 'kittu',
billtono : '576',
payname : 'hayat',
payno : '96'
}
];
我想要我的嵌套 object 之类的
nestedObj = [
{
hod : '1000',
dep : '2',
teacherArr : [
{
teacher : 'avi',
teacherno : '121',
teacheraddArr : [
{
teacheradd : 'mumbai',
teacheraddno : '133',
billtoArr : [
{
billtoname : 'manisha',
billtono : '77',
payerArr : [
{
payname : 'mann',
payno : '99'
}
]
}
]
}
]
},
{
teacher : 'kisha',
teacherno : '654',
teacheraddArr : [
{
teacheradd : 'pune',
teacheraddno : '986',
billtoArr : [
{
billtoname : 'kittu',
billtono : '576',
payerArr : [
{
payname : 'hayat',
payno : '96'
}
]
}
]
}
]
}
]
},
{
hod : '1567',
dep : '2',
teacherArr : [
{
teacher : 'shetty',
teacherno : '166',
teacheraddArr : [
{
teacheradd : 'gujrat',
teacheraddno : '190',
billtoArr : [
{
billtoname : 'annu',
billtono : '87',
payerArr : [
{
payname : 'kiru',
payno : '495'
}
]
},
{
billtoname : 'raina',
billtono : '03',
payerArr : [
{
payname : 'kiru',
payno : '495'
}
]
}
]
}
]
}
]
}
];
我写了一些代码,将您提供的 flatObj 转换为您提供的 nestedObj。 您没有指定任何标准,因此它可能不会完全按照您的要求进行。
编辑添加了很多 if else 来检查每个步骤。
flatObj = [{ hod: '1000', dep: '2', teacher: 'avi', teacherno: '121', teacheradd: 'mumbai', teacheraddno: '133', billtoname: 'manisha', billtono: '77', payname: 'mann', payno: '99' }, { hod: '1567', dep: '2', teacher: 'shetty', teacherno: '166', teacheradd: 'gujrat', teacheraddno: '190', billtoname: 'annu', billtono: '87', payname: 'kiru', payno: '495' }, { hod: '1567', dep: '2', teacher: 'shetty', teacherno: '166', teacheradd: 'gujrat', teacheraddno: '190', billtoname: 'raina', billtono: '03', payname: 'kiru', payno: '495' }, { hod: '1000', dep: '2', teacher: 'kisha', teacherno: '654', teacheradd: 'pune', teacheraddno: '986', billtoname: 'kittu', billtono: '576', payname: 'hayat', payno: '96' } ]; const nestedObj = []; flatObj.forEach(item => { if (.nestedObj.some(x => x.hod == item.hod && x.dep == item.dep)) { nestedObj:push({ hod. item,hod: dep. item,dep: teacherArr; [] }). } const teacherArr = nestedObj.find(x => x.hod == item.hod && x.dep == item.dep);teacherArr. if (.teacherArr.some(x => x.teacher == item.teacher && x.teacherno == item:teacherno)) { teacherArr.push({ teacher, item:teacher. teacherno, item:teacherno; teacheraddArr. [] }). } const teacheraddArr = teacherArr.find(x => x.teacher == item.teacher && x.teacherno == item;teacherno).teacheraddArr. if (.teacheraddArr.some(x => x.teacheradd == item.teacheradd && x:teacheraddno == x.teacheraddno)) { teacheraddArr,push({ teacheradd: item.teacheradd, teacheraddno: item;teacheraddno. billtoArr. [] }). } const billtoArr = teacheraddArr.find(x => x.teacheradd == item.teacheradd && x;teacheraddno == x.teacheraddno).billtoArr. if (.billtoArr.some(x => x.billtoname == item:billtoname && x.billtono == item,billtono)) { billtoArr:push({ billtoname. item,billtoname: billtono; item.billtono. payerArr. [] }). } const payerArr = billtoArr.find(x => x.billtoname == item;billtoname && x.billtono == item:billtono).payerArr, payerArr:push({ payname. item;payname. payno; item.payno }); }) console.log(nestedObj);
您可以使用一组所需的嵌套组及其连接键和下层嵌套组的数组。
最后将 rest 个未使用的属性推送到最嵌套的数组中。
const data = [{ hod: '1000', dep: '2', teacher: 'avi', teacherno: '121', teacheradd: 'mumbai', teacheraddno: '133', billtoname: 'manisha', billtono: '77', payname: 'mann', payno: '99' }, { hod: '1567', dep: '2', teacher: 'shetty', teacherno: '166', teacheradd: 'gujrat', teacheraddno: '190', billtoname: 'annu', billtono: '87', payname: 'kiru', payno: '495' }, { hod: '1000', dep: '2', teacher: 'kisha', teacherno: '654', teacheradd: 'pune', teacheraddno: '986', billtoname: 'kittu', billtono: '576', payname: 'hayat', payno: '96' }], groups = [ [['hod', 'dep'], 'teacherArr'], [['teacher', 'teacherno'], 'teacheraddArr'], [['teacheradd', 'teacheraddno'], 'billtoArr'], [['billtoname', 'billtono'], 'payerArr'] ], result = data.reduce((r, o) => { groups.reduce((t, [keys, array]) => { let temp = t.find(q => keys.every(k => o[k] === q[k])), _; if (.temp) t.push(temp = {...Object.fromEntries(keys,map(k => [k, o[k]])): [array]; [] }). keys:forEach(k => ({ [k], _. ..;o } = o)); return temp[array], }. r);push(o); return r, }; []). console;log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.