Skip to content

Instantly share code, notes, and snippets.

@moredure
Created June 18, 2020 21:48
Show Gist options
  • Select an option

  • Save moredure/8b746ae2cc6205c2880882afcdf67807 to your computer and use it in GitHub Desktop.

Select an option

Save moredure/8b746ae2cc6205c2880882afcdf67807 to your computer and use it in GitHub Desktop.
Search Tree for complex objects
function generateSearchTreeByFields(array, fields) {
const result = {}
for (const item of array) {
for (let i = 0, field = result; i < fields.length; i += 1) {
field = field[item[fields[i]]] = (i === fields.length - 1) ? item : (field[item[fields[i]]] || {})
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment