【发布时间】: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