Skip to content

Instantly share code, notes, and snippets.

@jsdbroughton
Created February 16, 2020 20:53
Show Gist options
  • Save jsdbroughton/3d46cb1bf4b05c7f0739cc344bc8a9e5 to your computer and use it in GitHub Desktop.
Save jsdbroughton/3d46cb1bf4b05c7f0739cc344bc8a9e5 to your computer and use it in GitHub Desktop.
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