使用 Lodash 合并复杂对象数组
Merge array of complex objects by using Lodash
我是 Lodash 的新手,正在尝试解决这个问题,但我可以找到解决问题的好方法。
我有一个对象数组 return 从数据库返回。数据结构如下:
var data = [{
index: 1,
Aoo: "a1",
Boo: 'b2',
}, {
index: 1,
Aoo: "a2",
Boo: 'b2',
}, {
index: 2,
Aoo: "a3",
Boo: 'b3',
}];
我想先按索引对对象进行分组,然后将属性 "Aoo" 和 "Boo" 分组并将其放入名为 param
.
的数组属性中
var result = [{
index: 1,
param: [{
Aoo: 'A1',
Boo: 'B1'
},{
Aoo: 'A2',
Boo: 'B2',
}]
}, {
index: 2,
param: [{
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), //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>
诚然,第二部分比分组稍微棘手一些,但幅度不大。由于索引为您提供了这种结构:
{
"1": [
{
"index": 1,
"Aoo": "a1",
"Boo": "b2"
},
{
"index": 1,
"Aoo": "a2",
"Boo": "b2"
}
],
"2": [
{
"index": 2,
"Aoo": "a3",
"Boo": "b3"
}
]
}
你真正想要做的就是检查它,让每个键成为 index
属性 然后你剩下一个初始对象数组。每个都需要通过删除 index
键进行转换,然后将其分配给 param
值。这是使用 .map
一次性完成的。不幸的是,它并不像看起来那么漂亮,但我认为这是 Lodash 基本版本所能做的最好的事情。
如果您想 select 键的特定子集,而不是 "all but index",那么您可以使用 _.pick
而不是 _.omit
来完成。这是它的样子:
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>
所以即使数据中有一个额外的键,我们也会得到相同的结果。
我是 Lodash 的新手,正在尝试解决这个问题,但我可以找到解决问题的好方法。
我有一个对象数组 return 从数据库返回。数据结构如下:
var data = [{
index: 1,
Aoo: "a1",
Boo: 'b2',
}, {
index: 1,
Aoo: "a2",
Boo: 'b2',
}, {
index: 2,
Aoo: "a3",
Boo: 'b3',
}];
我想先按索引对对象进行分组,然后将属性 "Aoo" 和 "Boo" 分组并将其放入名为 param
.
var result = [{
index: 1,
param: [{
Aoo: 'A1',
Boo: 'B1'
},{
Aoo: 'A2',
Boo: 'B2',
}]
}, {
index: 2,
param: [{
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), //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>
诚然,第二部分比分组稍微棘手一些,但幅度不大。由于索引为您提供了这种结构:
{
"1": [
{
"index": 1,
"Aoo": "a1",
"Boo": "b2"
},
{
"index": 1,
"Aoo": "a2",
"Boo": "b2"
}
],
"2": [
{
"index": 2,
"Aoo": "a3",
"Boo": "b3"
}
]
}
你真正想要做的就是检查它,让每个键成为 index
属性 然后你剩下一个初始对象数组。每个都需要通过删除 index
键进行转换,然后将其分配给 param
值。这是使用 .map
一次性完成的。不幸的是,它并不像看起来那么漂亮,但我认为这是 Lodash 基本版本所能做的最好的事情。
如果您想 select 键的特定子集,而不是 "all but index",那么您可以使用 _.pick
而不是 _.omit
来完成。这是它的样子:
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>
所以即使数据中有一个额外的键,我们也会得到相同的结果。