Created
June 18, 2020 21:48
-
-
Save moredure/8b746ae2cc6205c2880882afcdf67807 to your computer and use it in GitHub Desktop.
Search Tree for complex objects
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
| 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