【发布时间】:2017-03-16 13:16:02
【问题描述】:
我正在尝试自己映射嵌套对象,而不使用 JoinJS 之类的东西,这在一定程度上效果很好,直到我想正确地让我的 JSON 看起来很漂亮。
基于 SQL 查询,我的输出是:
[
{
"id": 1,
"city_name": "city 1 - some city name",
"city_category": "city 1 - some category",
"city_description": "city 1 - some city description",
"city_id": 1,
"store_id": 1
},
{
"id": 1,
"city_name": "city 1 - some city name",
"city_category": "city 1 - some category",
"city_description": "city 1 - some city description",
"city_id": 1,
"store_id": 2
},
{
"id": 1,
"city_name": "city 1 - some city name",
"city_category": "city 1 - some category",
"city_description": "city 1 - some city description",
"city_id": 1,
"store_id": 3
},
{
"id": 2,
"city_name": "city 2 - some city name",
"city_category": "city 2 - some category",
"city_description": "city 2 - some city description",
"city_id": 2,
"store_id": 1
},
{
"id": 2,
"city_name": "city 2 - some city name",
"city_category": "city 2 - some category",
"city_description": "city 2 - some city description",
"city_id": 2,
"store_id": 2
}
]
我试图让这个输出看起来像这样:
[
{
"city_id": 1,
"city_name": "city 1 - some city name",
"city_category": "city 1 - some category",
"city_description": "city 1 - some city description",
"store_ids": [1,2,3]
}
{
"city_id": 2,
"city_name": "city 2 - some city name",
"city_category": "city 2 - some category",
"city_description": "city 2 - some city description",
"store_ids": [1,2]
}
]
我的第一个猜测是我需要使用_.chain(result).uniqBy('city_id').value() 找到所有唯一的id 或city_id。但后来我有点坚持从输出中删除id,最重要的是,如何创建一个新键store_ids,它具有每个城市所有商店的数组。有意义吗?
使用Lodash 或者只是简单的 JS(没有像 JoinJS 这样的其他模块,除非它们对这样的事情很有效),我将如何操作它??
【问题讨论】:
标签: javascript json node.js lodash