1

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'
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
];
3
  • 1
    Why are you nesting? Commented Aug 4, 2020 at 14:13
  • so that no need to do filtering afterwards Commented Aug 4, 2020 at 16:12
  • @manoj If any of these answers solve your problem and you do not need any additional help, please consider accepting it. Thanks! Commented Aug 5, 2020 at 8:02

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

4 Comments

criteria is very hard to explain. thanks this is very helpful.
i updated my question by adding 1 criteria as you see in my flatObj array index 1 and 2 are same except billtoname and billtono but their payname and payno is same. so as you see in my nestedObj how my billtoArr should look like. Thanks in advance...
I edited the code to check each step if it already exists. This should do what you want.
Thank you sir. this is exactly what i wanted. Thanks alot.
2

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; }

1 Comment

Thank you.it works fine for me. Also something new concept i learned from this is the way you use groups array. thanks...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.