如何从平面对象创建嵌套的Json对象。如果不同对象的hod和dep代码相同,则添加相同的嵌套对象。 //////////////////////////////////////////////////// //////////////////////////////////////////////////// //////////////////////////////////////////////////// ///// 我的平面对象是== >>
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'
}
];
我希望我的嵌套对象像
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'
}
]
}
]
}
]
}
]
}
];
答案 0 :(得分:0)
我写了一些代码,将您提供的flatObj转换为您提供的nestedObj。您没有指定任何条件,因此它可能无法完全满足您的要求。
编辑添加了很多其他检查步骤。
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);
答案 1 :(得分:0)
您可以将所需的嵌套组的数组与下一个嵌套组的连接键和数组一起使用。
最后,将其余未使用的属性推到最嵌套的数组中。
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; }