如何通过嵌套值将嵌套对象的数组分组为多个数组 [英] How to Group Array of Nested Objects into Multiple Arrays by Nested Value
问题描述
我正在尝试通过值将数组转换为一组新数组,在本例中为id
.
I am trying to convert an array to a new set of arrays by value, in this case, id
.
输入
let array = [
{"item": {"id": 111, "name": "item1"}, "qty": 1},
{"item": {"id": 222, "name": "item2"}, "qty": 2},
{"item": {"id": 222, "name": "item3"}, "qty": 3}
];
所需的输出
let newArray = [
[{"item": {"id": 111, "name": "item1"}, "qty": 1}],
[{"item": {"id": 222, "name": "item2"}, "qty": 2},
{"item": {"id": 222, "name": "item3"}, "qty": 3}]
];
使用标准的groupBy
函数,我们可以返回按id
排序的两个数组.
Using a standard groupBy
function, we can return two arrays sorted by id
.
function groupItemBy(array, property) {
var hash = {};
for (var i = 0; i < array.length; i++) {
if (!hash[array[i][property]]) hash[array[i][property]] = [];
hash[array[i][property]].push(array[i]);
}
return hash;
}
但是,当尝试将它们映射到新数组时,嵌套的qty
数据会丢失.
However, when trying to map these to new arrays the nested qty
data is lost.
function parse() {
let tmp = Object.values(groupItemBy(array.map(el => el.item), "id"));
tmp.forEach(item => {
console.log(item);
// do something with each item in array
})
}
实际输出
let newArray = [
[{{"id": 111, "name": "item1"}],
[{"id": 222, "name": "item2"},
{"id": 222, "name": "item3"}]
];
在将原始数组分组为已排序数组的数组时,如何保持关联数据的完整性?
How can the integrity of the associated data be maintained while grouping the original array into an array of sorted arrays?
推荐答案
要使其正常工作,您将必须以某种方式告诉函数key属性的位置.可以想象非常复杂的嵌套对象,并且其中几个可能具有相同的属性名称,因此,如果没有这样的规范,它甚至可能导致歧义.
For this to work you will have to somehow tell the function where the key property is located. One can imagine very complex, nested objects, and several might have the same property names, so it even could lead to ambiguity if there is no such specification.
解决此问题的一种方法是使函数知道点分隔的属性(在一个字符串中)-一种路径".在您的情况下为item.id
.有了这些信息,函数就可以知道要在哪里查找id
值(在嵌套对象item
中).
One way to tackle this is to make the function aware of dot-separated properties (in one string) -- a kind of "path". In your case that would be item.id
. With that information the function can know where to look for the id
value (in the nested object item
).
很明显,该函数会将这些字符串除以这些点.然后,它可以对所得的属性名称数组执行reduce
,以找到数组中每个对象的键值.
Obviously the function would split that string by those dots. It can then perform a reduce
on the resulting array of property names to locate the key value for each object in the array.
这是它的外观:
let cart = [{"item": {"id": 111,"name": "item1", }, "qty": 10,}, {"item": {"id": 222,"name": "item2"},"qty": 1}, {"item": {"id": 222,"name": "item3"},"qty": 1,}];
function groupItemBy(array, property) {
var hash = {},
props = property.split('.');
for (var i = 0; i < array.length; i++) {
var key = props.reduce(function(acc, prop) {
return acc && acc[prop];
}, array[i]);
if (!hash[key]) hash[key] = [];
hash[key].push(array[i]);
}
return hash;
}
let grouped = Object.values(groupItemBy(cart, 'item.id'));
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这篇关于如何通过嵌套值将嵌套对象的数组分组为多个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!