Created
February 16, 2020 20:53
-
-
Save jsdbroughton/3d46cb1bf4b05c7f0739cc344bc8a9e5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const keyedItems = treeItems.map( ( item ): TreeItem => { | |
const fieldValues = fieldNames.map( ( field ): string => item.data && item.data[field] ); | |
let parentKey = fieldValues.slice( 0, -1 ).reduce( ( id, field ):string=>{ | |
if ( id && field ) { | |
id += `-` + field; | |
} | |
if ( !id && field ) { | |
id = field; | |
} | |
parentKeys.add( id ); | |
return id; | |
}, `` ); | |
let key = fieldValues.join( `-` ); | |
parentKeys.add( parentKey ); | |
// return the TreeItem with correct key and parentKey values | |
// ditch the mesh component of the TreeItem as unneeded baggage | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
const { mesh, ...keyedItem } = Object.assign( item.data, { id:item.id, name: item.name, parentKey, key } ); | |
return keyedItem; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment