Created
March 6, 2017 14:54
-
-
Save mordaha/cf335ba980b278dff6ac61d55f009d68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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