使用Lodash合并复杂对象的数组 [英] Merge array of complex objects by using Lodash

查看:251
本文介绍了使用Lodash合并复杂对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Lodash的新手,并试图解决这个问题,但可以找到一个很好的方法来做到这一点。



我有一个从数据库返回的对象数组。数据结构如下:

  var data = [{
index:1,
Aoo: a1,
Boo:'b2',
},{
指数:1,
Aoo:a2,
Boo:'b2',
},{
指数:2,
Aoo:a3,
Boo:'b3',
}];

我想先按索引对对象进行分组,然后将属性Aoo和Boo分组并将其放在名为 param 的数组属性中。

  var result = [{
index:1,
param:[{
Aoo:'A1',
Boo:'B1'
},{
Aoo: 'A2',
Boo:'B2',
}]
},{
指数:2,
参数:[{
Aoo:' A3',
Boo:'B3'
}]
}
]

我可以手动完成,但我想充分利用Lodash的功能。现在我只知道我可以通过使用 _。groupBy('index')实现分组的第一步,我仍然坚持下一步做什么。

解决方案

你快到了。以下是如何使用lodash



  var data = [{index :1,Aoo:a1,Boo:'b2',},{index:1,Aoo:a2,Boo:'b2',},{index:2,Aoo:a3,Boo:' b3',}]; var result = _.chain(data).groupBy('index')。map((value,key)=> {return {index:Number(key),//索引转换为一个字符串,这将使它再次成为一个数字.param:_. map(value,o => _.omit(o,'index'))//不包括前面对象的索引键}})。 value(); console.log(result);  

 < ; script src =https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js>< / script>  



第二部分是,adm但是,比分组稍微有些棘手,但不是很多。由于索引为您提供了这种结构:



  {1 :[{index:1,Aoo:a1,Boo:b2},{index:1,Aoo:a2,Boo:b2} ],2:[{index:2,Aoo:a3,Boo:b3}]}  



你真正想做的就是重复一遍,让每一个键成为 index 属性然后你会留下一系列初始物体。每个都需要通过删除索引键进行转换,并将其分配给 param 值。这是使用 .map 一次完成的。不幸的是,它并不像它看起来那么漂亮,但我认为这是你用Lodash的基本版本做的最好的。



如果你想选择一个特定的子集键,而不是除了索引,然后您可以使用 _。选择而不是 _。省略这样做。以下是它的外观:



  var data = [{index:1,Aoo:a1,Boo:'b2',Coo:c1},{index:1,Aoo:a2,Boo:'b2',Coo:c2},{ index:2,Aoo:a3,Boo:'b3',Coo:c3}]; var result = _.chain(data).groupBy('index')。map((value,key)=> ; {return {index:Number(key),//索引被转换为字符串,这将使它再次成为一个数字.param:_. map(value,o => _.pick(o,['Aoo) ','Boo']))//不包括前面对象的索引键}})。value(); console.log(result);  

< pre class =snippet-code-html lang-html prettyprint-override> < script src =https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1 /lodash.min.js\"></script>



因此,即使数据中有额外的密钥,我们也会得到相同的结果。


I'm new to Lodash and trying to solve this problem but could find a good way to do it.

I have an array of objects return back from the database. The data is structured as below:

var data = [{
    index: 1,
    Aoo: "a1",
    Boo: 'b2',
}, {
    index: 1,
    Aoo: "a2",
    Boo: 'b2',
}, {
    index: 2,
    Aoo: "a3",
    Boo: 'b3',
}];

I want to first group the objects by index, then group the attribute "Aoo" and "Boo" and put it in an array attribute called param.

var result = [{
        index: 1,
        param: [{
            Aoo: 'A1',
            Boo: 'B1'
        },{
            Aoo: 'A2',
            Boo: 'B2',
        }]
    }, {
        index: 2,
        param: [{
            Aoo: 'A3',
            Boo: 'B3'
        }]
   }
]

I can do it manually but I want to make the most use of Lodash's functionality. Right now I only know I can achieve the first step of grouping by using _.groupBy('index') and I am stuck at what to do next.

解决方案

You are almost there. Here is how this can be done using lodash

var data = [{
    index: 1,
    Aoo: "a1",
    Boo: 'b2',
}, {
    index: 1,
    Aoo: "a2",
    Boo: 'b2',
}, {
    index: 2,
    Aoo: "a3",
    Boo: 'b3',
}];

var result = _.chain(data)
  .groupBy('index')
  .map((value, key) => {
    return {
      index: Number(key), //the index was transformed into a string, this will make it a number again.
      param: _.map(value, o => _.omit(o, 'index'))//do not include the index key from the previous objects
    }
  })
  .value();

console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js"></script>

The second part is, admittedly, slightly trickier than the grouping, but not by much. Since indexing gives you this structure:

{
  "1": [
    {
      "index": 1,
      "Aoo": "a1",
      "Boo": "b2"
    },
    {
      "index": 1,
      "Aoo": "a2",
      "Boo": "b2"
    }
  ],
  "2": [
    {
      "index": 2,
      "Aoo": "a3",
      "Boo": "b3"
    }
  ]
}

All you really want to do is go over it, make each key be the index property and then you are left with an array of the inital objects. Each needs to be transformed by removing the index key and that will be assigned to the param value. This is done in one go using .map. Unfortunately, it's not as pretty as it could look but I think that's the best you can do with the base version of Lodash.

If you want to select a specific subset of the keys, instead of "all but index", then you can use _.pick instead of _.omit to do it. Here is how this would look:

var data = [{
    index: 1,
    Aoo: "a1",
    Boo: 'b2',
    Coo: "c1"
}, {
    index: 1,
    Aoo: "a2",
    Boo: 'b2',
    Coo: "c2"
}, {
    index: 2,
    Aoo: "a3",
    Boo: 'b3',
    Coo: "c3"
}];

var result = _.chain(data)
  .groupBy('index')
  .map((value, key) => {
    return {
      index: Number(key), //the index was transformed into a string, this will make it a number again.
      param: _.map(value, o => _.pick(o, ['Aoo', 'Boo']))//do not include the index key from the previous objects
    }
  })
  .value();

console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js"></script>

So we get the same result even though there is an extra key in the data.

这篇关于使用Lodash合并复杂对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆