Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Create an array which will store our results
- //`level` is a helper object to assist with search.
- //It means that the result array is considered a reference in `level.result`
- //so each iteration we build on top of `result`.
- let result = [];
- let level = { result };
- function reducer(accumulator, currentValue) {
- debugger;
- //If we already have an entry for this in the accumulator, we can just return it
- //Then, the next iteration of `currentValue` is known to be a child of this entry.
- if(accumulator[currentValue]) {
- return accumulator[currentValue];
- }
- //Otherwise, let's add this entry and then return it, again the next iteration
- //is going to be a child of this entry.
- accumulator[currentValue] = {
- result: []
- };
- //Note: everything in the accumulator is eventually discarded - we only care about the `result` which we are
- //continuously building from the ground up using references.
- let el = {
- name: currentValue,
- children: accumulator[currentValue].result
- };
- //push the current element to `result`, which is equal to the precedening element (parents) children. (??)
- accumulator.result.push(el);
- //For the next iteration, the accumulator will be set to accumulator[currentValue]
- return accumulator[currentValue];
- };
- paths.forEach(path => {
- let pathParts = path.split('/');
- //The accumulator will take the initial value of `level`
- pathParts.reduce(reducer, level)
- })
- //result has been continuously built upon - using the reference in the initial `level`.
- console.log(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement