Skip to content

Instantly share code, notes, and snippets.

@Akiyamka
Created January 10, 2019 15:01
Show Gist options
  • Save Akiyamka/904c352cc98c2b7f1c2e50b2949353d4 to your computer and use it in GitHub Desktop.
Save Akiyamka/904c352cc98c2b7f1c2e50b2949353d4 to your computer and use it in GitHub Desktop.
Create array of unique entries in dataset (not deep)
/**
* Create array of unique entries in dataset (not deep)
* @Example
* const data = [ { regionId: 123, indicatorId: 345 }, { regionId: 123, indicatorId: 345 }, { regionId: 678, indicatorId: 910 }]
* extractListFrom('regionId', 'indicatorId')(data);
*/
export function extractListFrom(...args) {
const accum = args.reduce((ac, arg) => { ac[arg] = new Set(); return ac }, {});
return data => data.reduce((acc, m) => {
Object.keys(acc).forEach(key => acc[key].add(m[key]))
return acc
}, accum)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment