I need to solve the following problem with a function that takes an array of objects as an argument and solves all three cases. Given an array of objects, how do I group them into sub-arrays based on several conditions? I am looking for errors in the payment system and want to receive an array of duplicated transactions (sorted by transaction time ascending).
Transactions are considered duplicates when: manufacturer, amount, category are exactly the same AND time between transactions is less than 45 seconds.
I am looking for an ES6 solution and I am sure that it would include .reduce method.
I tried working on it, by following reduce gives me an object based on manufacturer key, and this is not a result I would like to achieve as I need sub-arrays instead of objects and need more conditions than just a manufacturer.
let groupedArr = data.reduce((accumulator, currentValue) => {
accumulator[currentValue.manufacturer] = [...accumulator[currentValue.manufacturer] || [], currentValue];
return accumulator;
}, {});
Case 1:
INPUT:
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'
},
]
Expected output:
[[{
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'
}]
]
Case 2:
INPUT:
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'
}]
Expected output:
[]
Explanation: More than 45 seconds between transactions should output an empty array.
Case 3:
INPUT:
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'
}]
Expected output:
[]
Explanation: Less than 45 seconds, but category is different so it's not considered a duplicate.