weixin_41452476 2023-03-15 14:03 采纳率: 27.3%
浏览 73
已结题

把上面的数据格式改成下面这种格式的,给二级的每个对象里面加三个属性

baseGrowthTreeDTOList: [
        {
          nodeId: '87792989543447310672893548139712',
          nodeName: '一年级',
          childTreeList: [
            {
              nodeId: '87956107174299611063652915548692',
              nodeName: '一年级1班',
            },
            {
              nodeId: '87956107185871011064476605111923',
              nodeName: '一年级2班',
            },
          ],
        },
        {
          nodeId: '87793284944518910674821521775005',
          nodeName: '二年级',
          childTreeList: [
            {
              nodeId: '87956575445844911065467284430780',
              nodeName: '二年级1班',
            },
            {
              nodeId: '87956575457900911066295625034240',
              nodeName: '二年级2班',
            },
          ],
        },
      ],

把上面的数据格式改成下面这种格式的,给二级的每个对象里面加三个属性

```javascript

baseGrowthTreeDTOList: [
        {
          nodeId: '87792989543447310672893548139712',
          nodeName: '一年级',
          childTreeList: [
            {
              nodeId: '87956107174299611063652915548692',
              nodeName: '一年级1班',
              show: false,
              value: '',
              result: [],
            },
            {
              nodeId: '87956107185871011064476605111923',
              nodeName: '一年级2班',
              show: false,
              value: '',
              result: [],
            },
          ],
        },
        {
          nodeId: '87793284944518910674821521775005',
          nodeName: '二年级',
          childTreeList: [
            {
              nodeId: '87956575445844911065467284430780',
              nodeName: '二年级1班',
              show: false,
              value: '',
              result: [],
            },
            {
              nodeId: '87956575457900911066295625034240',
              nodeName: '二年级2班',
              show: false,
              value: '',
              result: [],
            },
          ],
        },
      ],

```

  • 写回答

6条回答 默认 最新

  • MarkHan_ 2023-03-15 14:08
    关注

    该回答引用GPTᴼᴾᴱᴺᴬᴵ
    可以使用数组的 forEach 方法和对象的解构来遍历并添加属性,可以按照如下方式实现:

    const data = {
      baseGrowthTreeDTOList: [
        {
          nodeId: '87792989543447310672893548139712',
          nodeName: '一年级',
          childTreeList: [
            {
              nodeId: '87956107174299611063652915548692',
              nodeName: '一年级1班',
            },
            {
              nodeId: '87956107185871011064476605111923',
              nodeName: '一年级2班',
            },
          ],
        },
        {
          nodeId: '87793284944518910674821521775005',
          nodeName: '二年级',
          childTreeList: [
            {
              nodeId: '87956575445844911065467284430780',
              nodeName: '二年级1班',
            },
            {
              nodeId: '87956575457900911066295625034240',
              nodeName: '二年级2班',
            },
          ],
        },
      ],
    };
    
    data.baseGrowthTreeDTOList.forEach((item) => {
      item.childTreeList.forEach((childItem) => {
        // 给每个二级对象添加三个属性
        childItem.show = false;
        childItem.value = '';
        childItem.result = [];
      });
    });
    
    console.log(data);
    
    
    

    输出结果如下:

    {
      baseGrowthTreeDTOList: [
        {
          nodeId: '87792989543447310672893548139712',
          nodeName: '一年级',
          childTreeList: [
            {
              nodeId: '87956107174299611063652915548692',
              nodeName: '一年级1班',
              show: false,
              value: '',
              result: []
            },
            {
              nodeId: '87956107185871011064476605111923',
              nodeName: '一年级2班',
              show: false,
              value: '',
              result: []
            }
          ]
        },
        {
          nodeId: '87793284944518910674821521775005',
          nodeName: '二年级',
          childTreeList: [
            {
              nodeId: '87956575445844911065467284430780',
              nodeName: '二年级1班',
              show: false,
              value: '',
              result: []
            },
            {
              nodeId: '87956575457900911066295625034240',
              nodeName: '二年级2班',
              show: false,
              value: '',
              result: []
            }
          ]
        }
      ]
    }
    
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月16日
  • 创建了问题 3月15日

悬赏问题

  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?