Reputation: 47
how do i create nested Json object from flat object. if hod and dep code is same for different objects then add in same nested object. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// my flat object is ==>>
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'
}
];
and i want my nested object like
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'
}
]
}
]
}
]
}
]
}
];
Upvotes: 1
Views: 133
Reputation: 386570
You could take an array of the wanted nested groups with their joined key and array for the lower nested group.
At the end push the rest of the unused properties to the most nested array.
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; }
Upvotes: 2
Reputation: 1898
I wrote some code that converts the flatObj you gave to the nestedObj you gave. You didn't specify any criteria so it might not do exactly what you want.
Edit added a whole lot of if else to check for each step.
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);
Upvotes: 2