Michael Profile Picture
Michael Malura
Entwickler - Fotograf - Technologie-Enthusiast

Ramda - Find duplicates in an array of objects

05.11.2018 code, ramda, javascript

For my current project i need to verify that a list of transactions doesn't contain any duplicates based on the transaction id. So i'll show you how i search for duplicates in lists based on a property.

const R = require('ramda');
const data = {
  'transactions': [{ // Transactions have to be unique
    'transaction': 'A',
    'events': [{
      'number': 0
    }, {
      'number': 1
    }]
  }, {
    'transaction': 'B',
    'events': [{
      'number': 0,
    }, {
      'number': 1,
    }]
  }]
};

const groupByTransaction = R.groupBy(R.prop('transaction'));
const mapLengthOfEachGroup = R.mapObjIndexed(R.prop('length'));

// Creates an array containing only the number of elements in each group. Best case would be [1, 1, 1...]
const arrayOfGroupLengths = R.values;

// Check if an element is greater than 1
const checkLengths = R.any(R.lte(2));

const hasDuplicates = R.pipe(
  groupByTransaction,
  mapLengthOfEachGroup,
  arrayOfGroupLengths,
  checkLengths
);

hasDuplicates(transactions);
Diese Seite verwendet Cookies. Wenn du damit einverstanden bist klicke bitte auf .