【问题标题】:Mapping/organizing nested objects in node映射/组织节点中的嵌套对象
【发布时间】: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() 找到所有唯一的idcity_id。但后来我有点坚持从输出中删除id,最重要的是,如何创建一个新键store_ids,它具有每个城市所有商店的数组。有意义吗?

使用Lodash 或者只是简单的 JS(没有像 JoinJS 这样的其他模块,除非它们对这样的事情很有效),我将如何操作它??

【问题讨论】:

    标签: javascript json node.js lodash


    【解决方案1】:

    我会遍历结果,并将它们分组到我的新对象结果中。 最后你会有一个对象的对象,键是item.id, 但是如果你想把它转换成数组很简单。

    let resultsObject = {};
    
    sqlQueryRows.map(function(item) {
       if (item.id in resultsObject) {
           resultsObject[item.id].store_ids.push(item.store_id);
       } else {
           resultsObject[item.id] = {
               city_id: item.city_id,
               city_name: item.city_name,
               city_category: item.city_category,
               city_description: item.city_description,
               store_ids: [item.store_id] 
           }
       }
    });
    
    // if you need an array
    let resultsArray = Object.values(resultsObject);
    

    【讨论】:

    • 从可读性和视觉组织的角度来看,这个答案对我来说最有意义。谢谢!!
    【解决方案2】:

    这是我的 lodash 方法:

    _(data)
      .uniqBy('city_id')
      .map(_.partialRight(_.omit, 'store_id'))
      .map(i => _.defaults({
        store_ids: _(data)
          .filter({ city_id: i.city_id })
          .map('store_id')
          .value()
      }, i))
      .value();
    

    这个想法是:

    • 获取独特的城市。
    • 删除存储 ID。
    • 通过按唯一城市 ID 过滤原始数据来创建存储 IDs 数组。

    【讨论】:

    • 感谢您的回答!!我最终使用了另一种方法(我没有意识到 lodash 会变得多么复杂),但是非常感谢您的帮助!在您输入之前,我什至不知道如何让 lodash 开始工作。
    【解决方案3】:

    如果您只是想删除某些字段,您可以使用 .map 方法,但我不确定我是否完全理解这个问题。

    var new_array = array_name.map((val) => {
        //do stuff to array
    }
    

    【讨论】:

    • 嗨 jsq324 感谢您回复我!我稍微编辑了我的问题。 我的第一个猜测是我需要使用_.chain(result).uniqBy('city_id').value() 找到所有唯一的idcity_id。但后来我有点坚持从输出中删除id,最重要的是,如何创建一个新键store_ids,其中包含每个城市所有商店的数组。
    猜你喜欢
    • 2022-08-10
    • 2021-02-08
    • 2023-01-23
    • 1970-01-01
    • 2020-09-27
    • 2013-08-01
    • 2017-05-14
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多