课程: JavaScript: Five Advanced Challenges and Concepts
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Solution: Make a nested array - JavaScript教程
课程: JavaScript: Five Advanced Challenges and Concepts
Solution: Make a nested array
(upbeat music) - [Instructor] To turn this flat array into a multidimensional array, we'll use the data map approach. So inside restructure array, the function I'll set up two constants. The first one is data map. Set it equal to an empty object. The second one is root. Set that to an empty array. At the end, we'll return the root array and then the root array will be picked up down here in the test code and output. Okay, the next step is to iterate through each of the items in the array and then place them inside the data map making the key of each of the items in the data map the same as the item ID. So I'll say data for each and then grab item and set a callback function. And here I say data map item ID equal to, and then the contents of the item itself and also the children array. Next, I'll iterate through all the items again, only this time I'll use the data map to figure out which item is the…