【问题标题】:Lodash and merge of Arrays with KeyLodash 和 Arrays 与 Key 的合并
【发布时间】:2019-04-30 00:15:57
【问题描述】:

我可以使用 lodash 很好地合并存储在数组中的对象,但我需要的是找到一种基于作为对象一部分的键进行合并的方法。如果我不使用密钥,则合并不可靠,因为当文档无序返回时,它将创建无效的合并。所以我希望有一种基于键值合并的方法,在我的例子中是它的 id。

这是一些示例:

文件 1

[
    {
         "id": 123,
         "Key1": "Test 1",
         "Key3": "Test 0"
    },
    {
         "id": 456,
         "Key1": "Test 2",
         "Key2": "Test 3"
    }
]

文件 2

[

    {
         "id": 123,
         "Key2": "Test 7",
         "Key3": "Test 8"
    },
    {
         "id": 789,
         "Key1": "Test 5",
         "Key2": "Test 6"
    }
]

根据上面的简单示例,我正在寻找这样的输出

[
    {
         "id": 123,
         "Key1": "Test 1",
         "Key2": "Test 7",
         "Key3": "Test 8"
    },
    {
         "id": 456,
         "Key1": "Test 2",
         "Key2": "Test 3"
    },
    {
         "id": 789,
         "Key1": "Test 5",
         "Key2": "Test 6"
    }
]

【问题讨论】:

    标签: lodash


    【解决方案1】:

    const addKeys = (results, keys) => keys.reduce(
      (final, item) => {
        const id = item.id;
        let current = final.find(i => i.id === id);
        if (!current) {
          current = { id: id };
          final.push(current);
        }
        Object.keys(item).forEach(key => { current[key] = item[key] });
        return final;
      }, results
    );
    
    console.log(addKeys(
      [
          {
               "id": 123,
               "Key1": "Test 1",
               "Key3": "Test 0"
          },
          {
               "id": 456,
               "Key1": "Test 2",
               "Key2": "Test 3"
          }
      ],
      [
    
          {
               "id": 123,
               "Key2": "Test 7",
               "Key3": "Test 8"
          },
          {
               "id": 789,
               "Key1": "Test 5",
               "Key2": "Test 6"
          }
      ]
    ));

    【讨论】:

    • 就像一个魅力,不知道为什么有人反对这个答案
    【解决方案2】:

    使用流创建函数。将数组连接到单个数组,按id 分组,然后映射组,并将每个组合并到单个对象:

    const { flow, concat, partialRight: pr, groupBy, map, merge } = _
    
    const mergeArrays = flow(
      concat, // concat to a single array
      pr(groupBy, 'id'), // group item by id
      pr(map, g => merge({}, ...g)) // merge each group to a single object
    )
    
    const arr1 = [{"id":123,"Key1":"Test 1","Key3":"Test 0"},{"id":456,"Key1":"Test 2","Key2":"Test 3"}]
    const arr2 = [{"id":123,"Key2":"Test 7","Key3":"Test 8"},{"id":789,"Key1":"Test 5","Key2":"Test 6"}]
    
    const result = mergeArrays(arr1, arr2)
    
    console.log(result)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

    这个解决方案的简洁版本带有lodash/fp

    const { flow, concat, groupBy, map, mergeAll } = _
    
    const mergeArrays = flow(
      concat, // concat to a single array
      groupBy('id'), // group item by id
      map(mergeAll) // merge each group to a single object
    )
    
    const arr1 = [{"id":123,"Key1":"Test 1","Key3":"Test 0"},{"id":456,"Key1":"Test 2","Key2":"Test 3"}]
    const arr2 = [{"id":123,"Key2":"Test 7","Key3":"Test 8"},{"id":789,"Key1":"Test 5","Key2":"Test 6"}]
    
    const result = mergeArrays(arr1, arr2)
    
    console.log(result)
    <script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 2014-06-04
      • 2016-06-12
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      相关资源
      最近更新 更多