合并和分组对​​象数组,不重复

时间:2017-07-01 05:18:33

标签: javascript lodash

我有一个重复的对象数组列表。他们每个人都有不同的财产。我想将属性唯一地分组到一个特定的数组对象而不重复数组对象。如果可以在lodash库中完成,那就太棒了。

数据

[  
   {  
      "mc":{  
         "id":"mc-id1"
      },
      "meal":{  
         "id":32,
         "mc_id":"mc-id1",
         "name":"BulletProof Vanilla"
      }
   },
   {  
      "mc":{  
         "id":"mc-id1",
      },
      "meal":{  
         "id":31,
         "mc_id":"mc-id1",
         "name":"Olive oil"
      }
   },
   {  
      "mc":{  
         "id":"mc-id2"
      },
      "meal":{  
         "id":38,
         "mc_id":"mc-id2",
         "name":"Organic fish tail"
      }
   },
   {  
      "mc":{  
         "id":"mc-id2"
      },
      "meal":{  
         "id":37,
         "mc_id":"mc-id2",
         "name":"Organic fish head"
      }
   }
]

预期输出

[  
   {  
      "mc":{  
         "id":"mc-id1"
      },
      "meal":[{  
         "id":32,
         "mc_id":"mc-id1",
         "name":"BulletProof Vanilla"
      },{
      "meal":{  
         "id":31,
         "mc_id":"mc-id1",
         "name":"Olive oil"
      }]
   },
   {  
      "mc":{  
         "id":"mc-id2"
      },
      "meal":[{  
         "id":38,
         "mc_id":"mc-id2",
         "name":"Organic fish tail"
      },{  
         "id":37,
         "mc_id":"mc-id2",
         "name":"Organic fish head"
      }]
   }
]

3 个答案:

答案 0 :(得分:0)

我先使用groupby,然后使用map来获得所需的输出。

var array = [  
	   {  
	      "mc":{  
	         "id":"mc-id1"
	      },
	      "meal":{  
	         "id":32,
	         "mc_id":"mc-id1",
	         "name":"BulletProof Vanilla"
	      }
	   },
	   {  
	      "mc":{  
	         "id":"mc-id1",
	      },
	      "meal":{  
	         "id":31,
	         "mc_id":"mc-id1",
	         "name":"Olive oil"
	      }
	   },
	   {  
	      "mc":{  
	         "id":"mc-id2"
	      },
	      "meal":{  
	         "id":38,
	         "mc_id":"mc-id2",
	         "name":"Organic fish tail"
	      }
	   },
	   {  
	      "mc":{  
	         "id":"mc-id2"
	      },
	      "meal":{  
	         "id":37,
	         "mc_id":"mc-id2",
	         "name":"Organic fish head"
	      }
	   }
	];
    var array = _.groupBy(array, function(o){
	  return o.mc.id;
	})

	array = _.map(array, function(o){
	  return {
	    mc : _.first(o).mc,
	    meal: _.map(o, function(p){
	    return p.meal
	  })
	  }
	})

	console.log(array)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

答案 1 :(得分:0)

作为一个可怕的一个班轮(假设你的输入是一个叫做数据的变量):

_.map(_.groupBy(data, (o) => o.mc.id), (i, k) => {return {mc: k, meal: _.map(i, (m) => m.meal)}})

稍微详细一点的方法:

var groupedByMc = _.groupBy(data, (obj) => obj.mc.id)
var output = _map(groupedByMc, (obj, key) => {
    return { 
        mc: key, 
        meal: _.map(obj, m) => m.meal 
    }
})

答案 2 :(得分:0)

  1. 您可以使用lodash#groupBymc.id对数组中的每个项目进行分组。

  2. lodash#mapValues与部分创建的lodash#map函数一起使用lodash#partial,将每个mc.idmeals相关联。

  3. 使用lodash#toPairsmc.id密钥和meal对象转换为键值对数组。例如{a:1, b:2}将为[['a', '1'],['b', '2']]

  4. 使用lodash#map使用部分创建的lodash#zipObjectDeep函数,使用lodash#partial将键值对数组分配到各自的键中:mc.id和{{1 }}

  5. meal

    &#13;
    &#13;
    var result = _(array)
      // group each item by mc.id key
      .groupBy('mc.id') 
      // associate each mc.id by all of the grouped meals
      .mapValues(_.partial(_.map, _, 'meal')) 
      // convert key-value into an array pair values
      .toPairs()
      // associate the array values into their respective keys
      .map(_.partial(_.zipObjectDeep, ['mc.id', 'meal']))
      // get the value
      .value();
    
    &#13;
    var array = [{
        "mc": {
          "id": "mc-id1"
        },
        "meal": {
          "id": 32,
          "mc_id": "mc-id1",
          "name": "BulletProof Vanilla"
        }
      },
      {
        "mc": {
          "id": "mc-id1",
        },
        "meal": {
          "id": 31,
          "mc_id": "mc-id1",
          "name": "Olive oil"
        }
      },
      {
        "mc": {
          "id": "mc-id2"
        },
        "meal": {
          "id": 38,
          "mc_id": "mc-id2",
          "name": "Organic fish tail"
        }
      },
      {
        "mc": {
          "id": "mc-id2"
        },
        "meal": {
          "id": 37,
          "mc_id": "mc-id2",
          "name": "Organic fish head"
        }
      }
    ];
    
    var result = _(array)
      // group each item by mc.id key
      .groupBy('mc.id') 
      // associate each mc.id by all of the grouped meals
      .mapValues(_.partial(_.map, _, 'meal')) 
      // convert key-value into an array pair values
      .toPairs()
      // associate the array values into their respective keys
      .map(_.partial(_.zipObjectDeep, ['mc.id', 'meal']))
      // get the value
      .value();
      
    console.log(result);
    &#13;
    body > div { min-height: 100%; top: 0; }
    &#13;
    &#13;
    &#13;