【问题标题】:How to change the structure of the object如何改变对象的结构
【发布时间】:2019-12-18 15:01:06
【问题描述】:

我从已转换成的数据库中检索到数据:

"items": {
    "title - nameSection": [
        {
            "nameBoard": "xxx",
            "idTask": 1,
            "nameTask": "xxx",
            "contentTask": "xxx",
            "complete": 1,
            "idSection": 1,
            "nameSection": "xxx",
            "contentSection": "xxx"
        },
        {
            "nameBoard": "xxx",
            "idTask": 2,
            "nameTask": "xxx",
            "contentTask": "xxx",
            "complete": 1,
            "idSection": 1,
            "nameSection": "xxx",
            "contentSection": ""
        }
    ],
    "title 2 - nameSection": [
        {
            "nameBoard": "xxx",
            "idTask": 3,
            "nameTask": "xxx",
            "contentTask": "",
            "complete": 0,
            "idSection": 2,
            "nameSection": "xxx",
            "contentSection": ""
        }
    ]
}

他使用lodash获得的:

   let grouped = groupBy(results, function (result) {
        return result.nameSection;
    })

我的结果是来自数据库的原始数据。 我注意到 lodash 将我的 'nameSection' 设置为 JSON 中的键,但是我不知道如何获得所需的效果。 我正在尝试使用 .map() (对吗?)为了得到这个结果:

"items": {
    "nameSection": "title - nameSection",
        "data": [
        {
            "nameBoard": "xxx",
            "idTask": 1,
            "nameTask": "xxx",
            "contentTask": "xxx",
            "complete": 1,
            "idSection": 1,
            "nameSection": "title - nameSection",
            "contentSection": "xxx"
        },
        {
            "nameBoard": "xxx",
            "idTask": 2,
            "nameTask": "xxx",
            "contentTask": "xxx",
            "complete": 1,
            "idSection": 1,
            "nameSection": "title - nameSection",
            "contentSection": ""
        }
    ],
        "nameSection": "title2 - nameSection", 
            "data": [
        {
            "nameBoard": "xxx",
            "idTask": 3,
            "nameTask": "xxx",
            "contentTask": "",
            "complete": 0,
            "idSection": 2,
            "nameSection": "title 2 - nameSection",
            "contentSection": ""
        }
    ]
}

对于如何解决问题的任何帮助和提示,我将不胜感激。

【问题讨论】:

  • items 应该是数组,对吧?否则您将无法使用地图。

标签: javascript arrays node.js json lodash


【解决方案1】:

将对象的条目映射到具有nameSection 键和data 数组的对象,同时覆盖数组中每个对象的nameSection 属性:

const items={"title - nameSection":[{nameBoard:"xxx",idTask:1,nameTask:"xxx",contentTask:"xxx",complete:1,idSection:1,nameSection:"xxx",contentSection:"xxx"},{nameBoard:"xxx",idTask:2,nameTask:"xxx",contentTask:"xxx",complete:1,idSection:1,nameSection:"xxx",contentSection:""}],"title 2 - nameSection":[{nameBoard:"xxx",idTask:3,nameTask:"xxx",contentTask:"",complete:0,idSection:2,nameSection:"xxx",contentSection:""}]};

const output = Object.entries(items)
  .map(([ nameSection, origData ]) => ({
    nameSection,
    data: origData.map(obj => ({ ...obj, nameSection }))
  }))
console.log(output);

【讨论】:

  • 感谢它对我有用!如果我想像 nameSection (数组外)那样提取 idSection 怎么办?
  • 取决于你想用它做什么,你在找什么?你想要idSection 作为父对象的键吗?对于可预测的输出,子数组中的每个对象可能需要具有相同的 idSection 值,是这样吗?如果是这样,我会在从外部.map 回调返回对象之前执行const { idSection } = origData[0]
猜你喜欢
  • 2016-02-06
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2018-08-16
  • 1970-01-01
  • 2015-02-04
  • 1970-01-01
相关资源
最近更新 更多