JavaScript - 根据多个属性将对象数组分组为子数组
JavaScript - Group array of objects into sub-arrays based on multiple properties
我需要使用一个函数来解决以下问题,该函数将对象数组作为参数并解决所有三种情况。
给定一个对象数组,我如何根据几个条件将它们分组到子数组中?我正在寻找支付系统中的错误,并希望收到一组重复的交易(按交易时间升序排序)。
在以下情况下交易被视为重复:制造商、金额、类别完全相同并且交易之间的时间少于 45 秒。
我正在寻找 ES6 解决方案,我确信它会包含 .reduce 方法。
我尝试过它,通过遵循 reduce 给我一个基于制造商密钥的对象,这不是我想要实现的结果,因为我需要子数组而不是对象并且需要更多条件而不仅仅是一个制造商。
let groupedArr = data.reduce((accumulator, currentValue) => {
accumulator[currentValue.manufacturer] = [...accumulator[currentValue.manufacturer] || [], currentValue];
return accumulator;
}, {});
案例一:
输入:
const data = [{
id: 3,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 4,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:38.000Z'
},
{
id: 1,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:00.000Z'
},
{
id: 7,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-20T11:00:00.000Z'
},
{
id: 6,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:44.000Z'
},
{
id: 2,
manufacturer: 'volkswagen',
amount: 2,
category: 'credit',
transaction: '2020-03-05T12:00:45.000Z'
},
{
id: 5,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:35:17.000Z'
},
]
预期输出:
[[{
id: 3,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 4,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:38.000Z'
},
{
id: 5,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:35:17.000Z'
}],
[{
id: 1,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:00.000Z'
},
{
id: 6,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:44.000Z'
}]
]
案例二:
输入:
const data = [{
id: 2,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 7,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-20T11:00:00.000Z'
}]
预期输出:
[]
说明:事务之间超过 45 秒应该输出一个空数组。
案例 3:
输入:
const data = [{
id: 2,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 1,
manufacturer: 'audi',
amount: 40,
category: 'credit',
transaction: '2020-03-02T10:34:40.000Z'
}]
预期输出:
[]
解释:不到 45 秒,但类别不同,因此不认为是重复的。
案例一:
function example1(initData, fieldsArr){
const output = data.reduce((aggObj, item) => {
const stringId = fieldsArr.map(key => item[key]).join('_');
if (aggObj[stringId]){
aggObj[stringId].push(item);
}
else {
aggObj[stringId] = [item];
}
return aggObj;
}, {})
const outputNoDups = Object.values(output).map(group => {
const sorted = group.sort((a,b) => new Date(a.transaction) < new Date(b.transaction) ? -1 : 1);
return sorted.filter((a, i) => {
if (i == 0) return true;
if (a.amount == sorted[i - 1].amount &&
new Date(a.transaction) - new Date(sorted[i - 1].transaction) <= 45000){
return true;
}
return false;
});
});
return outputNoDups.filter(a => a.length > 1);
}
console.log(example1(data, ['manufacturer', 'category']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script id="initData">
const data = [{
id: 3,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 4,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:38.000Z'
},
{
id: 1,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:00.000Z'
},
{
id: 7,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-20T11:00:00.000Z'
},
{
id: 6,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:44.000Z'
},
{
id: 2,
manufacturer: 'volkswagen',
amount: 2,
category: 'credit',
transaction: '2020-03-05T12:00:45.000Z'
},
{
id: 5,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:35:17.000Z'
},
];
</script>
输出(案例 1):
[
[
{
"id": 3,
"manufacturer": "audi",
"amount": 40,
"category": "leasing",
"transaction": "2020-03-02T10:34:30.000Z"
},
{
"id": 4,
"manufacturer": "audi",
"amount": 40,
"category": "leasing",
"transaction": "2020-03-02T10:34:38.000Z"
},
{
"id": 5,
"manufacturer": "audi",
"amount": 40,
"category": "leasing",
"transaction": "2020-03-02T10:35:17.000Z"
}
],
[
{
"id": 1,
"manufacturer": "mercedes",
"amount": 20,
"category": "leasing",
"transaction": "2020-03-05T12:00:00.000Z"
},
{
"id": 6,
"manufacturer": "mercedes",
"amount": 20,
"category": "leasing",
"transaction": "2020-03-05T12:00:44.000Z"
}
]
]
案例二:
function example1(initData, fieldsArr){
const output = data.reduce((aggObj, item) => {
const stringId = fieldsArr.map(key => item[key]).join('_');
if (aggObj[stringId]){
aggObj[stringId].push(item);
}
else {
aggObj[stringId] = [item];
}
return aggObj;
}, {})
const outputNoDups = Object.values(output).map(group => {
const sorted = group.sort((a,b) => new Date(a.transaction) < new Date(b.transaction) ? -1 : 1);
return sorted.filter((a, i) => {
if (i == 0) return true;
if (a.amount == sorted[i - 1].amount &&
new Date(a.transaction) - new Date(sorted[i - 1].transaction) <= 45000){
return true;
}
return false;
});
});
return outputNoDups.filter(a => a.length > 1);
}
console.log(example1(data, ['manufacturer', 'category']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script id="initData">
const data = [{
id: 2,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 7,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-20T11:00:00.000Z'
}]
</script>
案例三:
function example1(initData, fieldsArr){
const output = data.reduce((aggObj, item) => {
const stringId = fieldsArr.map(key => item[key]).join('_');
if (aggObj[stringId]){
aggObj[stringId].push(item);
}
else {
aggObj[stringId] = [item];
}
return aggObj;
}, {})
const outputNoDups = Object.values(output).map(group => {
const sorted = group.sort((a,b) => new Date(a.transaction) < new Date(b.transaction) ? -1 : 1);
return sorted.filter((a, i) => {
if (i == 0) return true;
if (a.amount == sorted[i - 1].amount &&
new Date(a.transaction) - new Date(sorted[i - 1].transaction) <= 45000){
return true;
}
return false;
});
});
return outputNoDups.filter(a => a.length > 1);
}
console.log(example1(data, ['manufacturer', 'category']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script id="initData">
const data = [{
id: 2,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 1,
manufacturer: 'audi',
amount: 40,
category: 'credit',
transaction: '2020-03-02T10:34:40.000Z'
}]
</script>
情况 4(您没有考虑的边缘情况 - 时间小于 45 但数量不同):
function example1(initData, fieldsArr){
const output = data.reduce((aggObj, item) => {
const stringId = fieldsArr.map(key => item[key]).join('_');
if (aggObj[stringId]){
aggObj[stringId].push(item);
}
else {
aggObj[stringId] = [item];
}
return aggObj;
}, {})
const outputNoDups = Object.values(output).map(group => {
const sorted = group.sort((a,b) => new Date(a.transaction) < new Date(b.transaction) ? -1 : 1);
return sorted.filter((a, i) => {
if (i == 0) return true;
if (a.amount == sorted[i - 1].amount &&
new Date(a.transaction) - new Date(sorted[i - 1].transaction) <= 45000){
return true;
}
return false;
});
});
return outputNoDups.filter(a => a.length > 1);
}
console.log(example1(data, ['manufacturer', 'category']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script id="initData">
const data = [{
id: 2,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 1,
manufacturer: 'audi',
amount: 30,
category: 'leasing',
transaction: '2020-03-02T10:34:40.000Z'
}]
</script>
我需要使用一个函数来解决以下问题,该函数将对象数组作为参数并解决所有三种情况。 给定一个对象数组,我如何根据几个条件将它们分组到子数组中?我正在寻找支付系统中的错误,并希望收到一组重复的交易(按交易时间升序排序)。
在以下情况下交易被视为重复:制造商、金额、类别完全相同并且交易之间的时间少于 45 秒。
我正在寻找 ES6 解决方案,我确信它会包含 .reduce 方法。
我尝试过它,通过遵循 reduce 给我一个基于制造商密钥的对象,这不是我想要实现的结果,因为我需要子数组而不是对象并且需要更多条件而不仅仅是一个制造商。
let groupedArr = data.reduce((accumulator, currentValue) => {
accumulator[currentValue.manufacturer] = [...accumulator[currentValue.manufacturer] || [], currentValue];
return accumulator;
}, {});
案例一:
输入:
const data = [{
id: 3,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 4,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:38.000Z'
},
{
id: 1,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:00.000Z'
},
{
id: 7,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-20T11:00:00.000Z'
},
{
id: 6,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:44.000Z'
},
{
id: 2,
manufacturer: 'volkswagen',
amount: 2,
category: 'credit',
transaction: '2020-03-05T12:00:45.000Z'
},
{
id: 5,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:35:17.000Z'
},
]
预期输出:
[[{
id: 3,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 4,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:38.000Z'
},
{
id: 5,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:35:17.000Z'
}],
[{
id: 1,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:00.000Z'
},
{
id: 6,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:44.000Z'
}]
]
案例二:
输入:
const data = [{
id: 2,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 7,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-20T11:00:00.000Z'
}]
预期输出:
[]
说明:事务之间超过 45 秒应该输出一个空数组。
案例 3:
输入:
const data = [{
id: 2,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 1,
manufacturer: 'audi',
amount: 40,
category: 'credit',
transaction: '2020-03-02T10:34:40.000Z'
}]
预期输出:
[]
解释:不到 45 秒,但类别不同,因此不认为是重复的。
案例一:
function example1(initData, fieldsArr){
const output = data.reduce((aggObj, item) => {
const stringId = fieldsArr.map(key => item[key]).join('_');
if (aggObj[stringId]){
aggObj[stringId].push(item);
}
else {
aggObj[stringId] = [item];
}
return aggObj;
}, {})
const outputNoDups = Object.values(output).map(group => {
const sorted = group.sort((a,b) => new Date(a.transaction) < new Date(b.transaction) ? -1 : 1);
return sorted.filter((a, i) => {
if (i == 0) return true;
if (a.amount == sorted[i - 1].amount &&
new Date(a.transaction) - new Date(sorted[i - 1].transaction) <= 45000){
return true;
}
return false;
});
});
return outputNoDups.filter(a => a.length > 1);
}
console.log(example1(data, ['manufacturer', 'category']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script id="initData">
const data = [{
id: 3,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 4,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:38.000Z'
},
{
id: 1,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:00.000Z'
},
{
id: 7,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-20T11:00:00.000Z'
},
{
id: 6,
manufacturer: 'mercedes',
amount: 20,
category: 'leasing',
transaction: '2020-03-05T12:00:44.000Z'
},
{
id: 2,
manufacturer: 'volkswagen',
amount: 2,
category: 'credit',
transaction: '2020-03-05T12:00:45.000Z'
},
{
id: 5,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:35:17.000Z'
},
];
</script>
输出(案例 1):
[
[
{
"id": 3,
"manufacturer": "audi",
"amount": 40,
"category": "leasing",
"transaction": "2020-03-02T10:34:30.000Z"
},
{
"id": 4,
"manufacturer": "audi",
"amount": 40,
"category": "leasing",
"transaction": "2020-03-02T10:34:38.000Z"
},
{
"id": 5,
"manufacturer": "audi",
"amount": 40,
"category": "leasing",
"transaction": "2020-03-02T10:35:17.000Z"
}
],
[
{
"id": 1,
"manufacturer": "mercedes",
"amount": 20,
"category": "leasing",
"transaction": "2020-03-05T12:00:00.000Z"
},
{
"id": 6,
"manufacturer": "mercedes",
"amount": 20,
"category": "leasing",
"transaction": "2020-03-05T12:00:44.000Z"
}
]
]
案例二:
function example1(initData, fieldsArr){
const output = data.reduce((aggObj, item) => {
const stringId = fieldsArr.map(key => item[key]).join('_');
if (aggObj[stringId]){
aggObj[stringId].push(item);
}
else {
aggObj[stringId] = [item];
}
return aggObj;
}, {})
const outputNoDups = Object.values(output).map(group => {
const sorted = group.sort((a,b) => new Date(a.transaction) < new Date(b.transaction) ? -1 : 1);
return sorted.filter((a, i) => {
if (i == 0) return true;
if (a.amount == sorted[i - 1].amount &&
new Date(a.transaction) - new Date(sorted[i - 1].transaction) <= 45000){
return true;
}
return false;
});
});
return outputNoDups.filter(a => a.length > 1);
}
console.log(example1(data, ['manufacturer', 'category']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script id="initData">
const data = [{
id: 2,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 7,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-20T11:00:00.000Z'
}]
</script>
案例三:
function example1(initData, fieldsArr){
const output = data.reduce((aggObj, item) => {
const stringId = fieldsArr.map(key => item[key]).join('_');
if (aggObj[stringId]){
aggObj[stringId].push(item);
}
else {
aggObj[stringId] = [item];
}
return aggObj;
}, {})
const outputNoDups = Object.values(output).map(group => {
const sorted = group.sort((a,b) => new Date(a.transaction) < new Date(b.transaction) ? -1 : 1);
return sorted.filter((a, i) => {
if (i == 0) return true;
if (a.amount == sorted[i - 1].amount &&
new Date(a.transaction) - new Date(sorted[i - 1].transaction) <= 45000){
return true;
}
return false;
});
});
return outputNoDups.filter(a => a.length > 1);
}
console.log(example1(data, ['manufacturer', 'category']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script id="initData">
const data = [{
id: 2,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 1,
manufacturer: 'audi',
amount: 40,
category: 'credit',
transaction: '2020-03-02T10:34:40.000Z'
}]
</script>
情况 4(您没有考虑的边缘情况 - 时间小于 45 但数量不同):
function example1(initData, fieldsArr){
const output = data.reduce((aggObj, item) => {
const stringId = fieldsArr.map(key => item[key]).join('_');
if (aggObj[stringId]){
aggObj[stringId].push(item);
}
else {
aggObj[stringId] = [item];
}
return aggObj;
}, {})
const outputNoDups = Object.values(output).map(group => {
const sorted = group.sort((a,b) => new Date(a.transaction) < new Date(b.transaction) ? -1 : 1);
return sorted.filter((a, i) => {
if (i == 0) return true;
if (a.amount == sorted[i - 1].amount &&
new Date(a.transaction) - new Date(sorted[i - 1].transaction) <= 45000){
return true;
}
return false;
});
});
return outputNoDups.filter(a => a.length > 1);
}
console.log(example1(data, ['manufacturer', 'category']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script id="initData">
const data = [{
id: 2,
manufacturer: 'audi',
amount: 40,
category: 'leasing',
transaction: '2020-03-02T10:34:30.000Z'
},
{
id: 1,
manufacturer: 'audi',
amount: 30,
category: 'leasing',
transaction: '2020-03-02T10:34:40.000Z'
}]
</script>