【问题标题】:Merge rows residing inside json array based on certain Keys根据某些键合并驻留在 json 数组中的行
【发布时间】:2019-07-11 16:02:20
【问题描述】:

寻找问题的解决方案:

需要合并以下数组,使得最终输出只有一行对应每个唯一的类别和子类别组合,其余字段(主题和个人)合并为一个数组:

输入:

[
   {
      "category":"cat2",
      "subcategory":"b",
      "personal":[
         "inclsn2"
      ],
      "topic":[
         "topic1",
         "topic3"
      ]
   },
   {
      "category":"cat1",
      "subcategory":"a",
      "personal":[
         "inclsn2"
      ],
      "topic":[
         "topic4"
      ]
   },
   {
      "category":"cat2",
      "subcategory":"b",
      "personal":[
         "inclsn2"
      ],
      "topic":[
         "topic5",
         "topic2"
      ]
   },
   {
      "category":"cat2",
      "subcategory":"b",
      "personal":[
         "inclsn2"
      ],
      "topic":[
         "topic1",
         "topic2"
      ]
   },
   {
      "category":"cat2",
      "subcategory":"b",
      "personal":[
         "inclsn2"
      ],
      "topic":[
         "topic1",
         "topic2"
      ]
   },
   {
      "category":"cat1",
      "subcategory":"a",
      "personal":[
         "inclsn2"
      ],
      "topic":[
         "topic1",
         "topic2"
      ]
   },
   {
      "category":"cat1",
      "subcategory":"a",
      "personal":[
         "inclsn2"
      ],
      "topic":[
         "topic1",
         "topic2"
      ]
   },
   {
      "category":"cat3",
      "subcategory":"c",
      "personal":[
         "inclsn3"
      ],
      "topic":[
         "topic1",
         "topic2"
      ]
   },
   {
      "category":"cat1",
      "subcategory":"a",
      "personal":[
         "inclsn2"
      ],
      "topic":[
         "topic1",
         "topic2"
      ]
   },
   {
      "category":"cat1",
      "subcategory":"a",
      "personal":null,
      "topic":[
         "topic6"
      ]
   }
]

预期输出:

[{"category": "cat2", "subcategory": "b", "personal": ["inclsn2"], "topic": ["topic2","topic5","topic4","topic1"]},
{"category": "cat1", "subcategory": "a", "personal": ["inclsn2"], "topic": ["topic6","topic2","topic4","topic1"]},
{"category": "cat3", "subcategory": "c", "personal": ["inclsn3"], "topic": ["topic1","topic2"]}]

对此的任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: javascript node.js merge group-by


    【解决方案1】:

    您的问题的一个解决方案是过滤数组,条件是以前看不到类别/子类别组合。您可以使用一个对象,其键是类别/子类别的某种组合(例如,与某些分隔符连接)并查看该对象是否具有给定的键。

    你可以这样做:

    const input = [] // your array here
    
    const d = '\t';
    const merge = (a, c = {}) => a.filter((e, f, k) =>
                  (k = e.category + d + e.subcategory, f = c[k], c[k] = 1, !f));
    
    console.log(merge(input));
    

    确保将d 设置为永远不会成为真实类别或子类别名称一部分的字符串 - 这里它是一个制表符,但您可以将其设置为其他任何值。

    数组的输出:

    [ { category: 'cat2',
        subcategory: 'b',
        personal: [ 'inclsn2' ],
        topic: [ 'topic1', 'topic3' ] },
      { category: 'cat1',
        subcategory: 'a',
        personal: [ 'inclsn2' ],
        topic: [ 'topic4' ] },
      { category: 'cat3',
        subcategory: 'c',
        personal: [ 'inclsn3' ],
        topic: [ 'topic1', 'topic2' ] } ]
    

    如果您使用 Lodash,那么您可以进一步简化您的 merge() 函数:

    const _ = require('lodash');
    
    const d = '\t';
    const merge = a => _.uniqBy(a, e => e.category + d + e.subcategory);
    
    console.log(merge(input));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 2019-01-13
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多