【问题标题】:javascript nested Json object from flat object来自平面对象的javascript嵌套Json对象
【发布时间】:2020-08-04 14:08:39
【问题描述】:

如何从平面对象创建嵌套的 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'
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
];

【问题讨论】:

  • 你为什么要嵌套?
  • 这样以后就不需要过滤了
  • @manoj 如果这些答案中的任何一个解决了您的问题并且您不需要任何其他帮助,请考虑接受它。谢谢!

标签: javascript arrays json object nested


【解决方案1】:

我编写了一些代码,将您提供的 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);

【讨论】:

  • 标准很难解释。谢谢,这很有帮助。
  • 我通过添加 1 个条件来更新我的问题,正如您在我的 flatObj 数组中看到的那样,索引 1 和 2 除了 billtoname 和 billtono 之外是相同的,但他们的 payname 和 payno 是相同的。正如您在我的nestedObj 中看到的,我的 billtoArr 应该是什么样子。在此先感谢...
  • 我编辑了代码来检查每个步骤是否已经存在。这应该做你想做的。
  • 谢谢先生。这正是我想要的。非常感谢。
【解决方案2】:

您可以将所需嵌套组的数组及其连接键和数组用于较低的嵌套组。

最后将其余未使用的属性推送到最嵌套的数组。

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

【讨论】:

  • 谢谢。它对我来说很好用。我从中学到的一些新概念是您使用组数组的方式。谢谢...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 2021-10-19
  • 2012-05-29
  • 2020-06-23
相关资源
最近更新 更多