Graffis
Graffis

Reputation: 45

JavaScript - Group array of objects into sub-arrays based on multiple properties

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.

Upvotes: 1

Views: 991

Answers (1)

Alex L
Alex L

Reputation: 4241

Case 1:

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>

OUTPUT (Case 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"
    }
  ]
]

Case 2:

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>

Case 3:

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>

Case 4 (an edge case you hadn't considered - time is less than 45 but amount is different):

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>

Upvotes: 1

Related Questions