Skip to content

Instantly share code, notes, and snippets.

@mordaha
Created March 6, 2017 14:54
Show Gist options
  • Save mordaha/cf335ba980b278dff6ac61d55f009d68 to your computer and use it in GitHub Desktop.
Save mordaha/cf335ba980b278dff6ac61d55f009d68 to your computer and use it in GitHub Desktop.
// converts array of items into object of items with item.field as a key
export const byField = (field, list) => {
if (list && list.length > 0) {
return list.reduce((acc, item) => {
const keys = acc.keys || [];
return {
...acc,
keys: [...keys, item[field]],
[item[field]]: { ...item },
};
}, {});
}
return {
keys: [],
};
};
// converts byId dict maked by above function to array
export const toArray = byIdDict => (
byIdDict && byIdDict.keys
?
byIdDict.keys.map(key => ({ ...byIdDict[key] }))
:
[]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment