Skip to content

Instantly share code, notes, and snippets.

@lxm7
Created February 9, 2019 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lxm7/64951f569264414d67ec64824297c1c4 to your computer and use it in GitHub Desktop.
Save lxm7/64951f569264414d67ec64824297c1c4 to your computer and use it in GitHub Desktop.
Merge 2 arrays v.efficiently set against the match of one field from one and one field form the other set. Maintain properties from both
const rootResolvers = {
Query: {
coins: async (_, { limit, offset }) => {
const initialLimit = process.env.LIMIT || limit;
const initialOffset = offset || 0;
const cmc = await fetchCmcListFn(initialLimit, initialOffset);
const cc = await fetchCcListFn();
const coinListWithCcId = lodash(cmc) // start sequence
.keyBy('symbol') // create a dictionary of the 1st array
.merge(lodash.keyBy(cc, 'Symbol')) // create a dictionary of the 2nd array, and merge it to the 1st
.values() // turn the combined dictionary to array
.value() // get the value (array) out of the sequence
.filter((coin) => typeof coin.cmc_rank !== 'undefined'); // remove coins without rank value
return coinListWithCcId;
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment