For my current project I need to check that a list of transactions contains no duplicates (based on the transaction ID).
Here's 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,
}]
}]
};
// Group each transactions by its transaction id
const groupByTransaction = R.groupBy(R.prop('transaction'));
// Get the length of each transaction group
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 ensureArrayContainsOnlyOnes = R.any(R.lte(2));
const hasDuplicates = R.pipe(
groupByTransaction,
mapLengthOfEachGroup,
arrayOfGroupLengths,
ensureArrayContainsOnlyOnes
);
hasDuplicates(data.transactions);
05.11.2018
updated 27.06.2026