按子文档值分组对象 - 调整 Array.reduce 方法(元编程?)
Grouping Objects by subdocument value - Adapting the Array.reduce method (metaprogramming?)
我找到了关于如何使用 reduce 进行分组的明确答案。
Most efficient method to groupby on an array of objects
const array = [
{ _id:" 5fbcca92c36b24d99395623d",
assetData:
{ _id: "5fbcca159ae5f2d8ed792542",
name: 'House 1',
numberOfRooms: '1',
numberOfBathrooms: '2' } },
{ _id: "5fbcca92c36b24d99395623c",
assetData:
{ _id: "5fbcca159ae5f2d8ed792542",
name: 'House 1',
numberOfRooms: '1',
numberOfBathrooms: '2' } },
{ _id: "5fbccb32c36b24d993956242",
assetData:
{ _id: "5fbbedcb4decb58513e4ba80",
name: 'House 2',
numberOfRooms: '1',
numberOfBathrooms: '1' } },
{ _id: "5fbccb32c36b24d993956243",
assetData:
{ _id: "5fbbedcb4decb58513e4ba80",
name: 'House 2',
numberOfRooms: '1',
numberOfBathrooms: '1' } },
];
const groupBy = function (xs, key) {
return xs.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
const test = Object.entries(groupBy(array, "_id"));
console.log(test);
我正在尝试适应,而不是按 _id
排序,而是按 assetData._id
排序。更改 groupBy
函数的 key
参数,不要输出行为。它不接受子文档的值。我什至不知道如何继续我的研究,因为我无法真正理解函数 (rv[x[key]] = rv[x[key]] || [])
这部分的含义。我开始阅读有关 ES6 中的元编程的内容,但我不确定它是否会引导我找到答案。有什么建议吗?
好吧,由于我还没有理论上的答案,所以我尝试以实际的方式解决问题。似乎我无法将字符串 assetData._id
直接传递给 key
参数。也许稍后有人会弹出原因。现在,为了获得我预期的结果,我将密钥拆分为密钥和 subkey
,并通过 assetData
与 _id
分开。最终代码为:
const array = [
{ _id:" 5fbcca92c36b24d99395623d",
assetData:
{ _id: "5fbcca159ae5f2d8ed792542",
name: 'House 1',
numberOfRooms: '1',
numberOfBathrooms: '2' } },
{ _id: "5fbcca92c36b24d99395623c",
assetData:
{ _id: "5fbcca159ae5f2d8ed792542",
name: 'House 1',
numberOfRooms: '1',
numberOfBathrooms: '2' } },
{ _id: "5fbccb32c36b24d993956242",
assetData:
{ _id: "5fbbedcb4decb58513e4ba80",
name: 'House 2',
numberOfRooms: '1',
numberOfBathrooms: '1' } },
{ _id: "5fbccb32c36b24d993956243",
assetData:
{ _id: "5fbbedcb4decb58513e4ba80",
name: 'House 2',
numberOfRooms: '1',
numberOfBathrooms: '1' } },
];
const groupBy = function (xs, key, subkey) {
return xs.reduce(function (rv, x) {
(rv[x[key][subkey]] = rv[x[key][subkey]] || []).push(x);
return rv;
}, {});
};
const test = Object.entries(groupBy(array, "assetData", "_id"))
console.log(test)
我愿意接受更开明的答案!
我找到了关于如何使用 reduce 进行分组的明确答案。
Most efficient method to groupby on an array of objects
const array = [
{ _id:" 5fbcca92c36b24d99395623d",
assetData:
{ _id: "5fbcca159ae5f2d8ed792542",
name: 'House 1',
numberOfRooms: '1',
numberOfBathrooms: '2' } },
{ _id: "5fbcca92c36b24d99395623c",
assetData:
{ _id: "5fbcca159ae5f2d8ed792542",
name: 'House 1',
numberOfRooms: '1',
numberOfBathrooms: '2' } },
{ _id: "5fbccb32c36b24d993956242",
assetData:
{ _id: "5fbbedcb4decb58513e4ba80",
name: 'House 2',
numberOfRooms: '1',
numberOfBathrooms: '1' } },
{ _id: "5fbccb32c36b24d993956243",
assetData:
{ _id: "5fbbedcb4decb58513e4ba80",
name: 'House 2',
numberOfRooms: '1',
numberOfBathrooms: '1' } },
];
const groupBy = function (xs, key) {
return xs.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
const test = Object.entries(groupBy(array, "_id"));
console.log(test);
我正在尝试适应,而不是按 _id
排序,而是按 assetData._id
排序。更改 groupBy
函数的 key
参数,不要输出行为。它不接受子文档的值。我什至不知道如何继续我的研究,因为我无法真正理解函数 (rv[x[key]] = rv[x[key]] || [])
这部分的含义。我开始阅读有关 ES6 中的元编程的内容,但我不确定它是否会引导我找到答案。有什么建议吗?
好吧,由于我还没有理论上的答案,所以我尝试以实际的方式解决问题。似乎我无法将字符串 assetData._id
直接传递给 key
参数。也许稍后有人会弹出原因。现在,为了获得我预期的结果,我将密钥拆分为密钥和 subkey
,并通过 assetData
与 _id
分开。最终代码为:
const array = [
{ _id:" 5fbcca92c36b24d99395623d",
assetData:
{ _id: "5fbcca159ae5f2d8ed792542",
name: 'House 1',
numberOfRooms: '1',
numberOfBathrooms: '2' } },
{ _id: "5fbcca92c36b24d99395623c",
assetData:
{ _id: "5fbcca159ae5f2d8ed792542",
name: 'House 1',
numberOfRooms: '1',
numberOfBathrooms: '2' } },
{ _id: "5fbccb32c36b24d993956242",
assetData:
{ _id: "5fbbedcb4decb58513e4ba80",
name: 'House 2',
numberOfRooms: '1',
numberOfBathrooms: '1' } },
{ _id: "5fbccb32c36b24d993956243",
assetData:
{ _id: "5fbbedcb4decb58513e4ba80",
name: 'House 2',
numberOfRooms: '1',
numberOfBathrooms: '1' } },
];
const groupBy = function (xs, key, subkey) {
return xs.reduce(function (rv, x) {
(rv[x[key][subkey]] = rv[x[key][subkey]] || []).push(x);
return rv;
}, {});
};
const test = Object.entries(groupBy(array, "assetData", "_id"))
console.log(test)
我愿意接受更开明的答案!