课程: JavaScript: Five Advanced Challenges and Concepts
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Managing nested comments - JavaScript教程
课程: JavaScript: Five Advanced Challenges and Concepts
Managing nested comments
- A common challenge you'll come across when working with data sources is managing nesting of dynamic data. The classic example of this is nested comments in other conversational threads. Here's the scenario. Your data source outputs a flat array of objects with common data. Each object has an ID, a parent ID, and the comment data. Your challenge is to turn the flat array into a hierarchical presentation of the comment so that each comment is nested under the other one. This is tricky precisely because the original data is a flat array, meaning you have to somehow iterate through each item in the array, figure out if it's a parent or a child, then if it's a child, append the item to the correct parent, all the while keeping in mind a child may also be a parent for another item. There are several ways of solving this real world problem. Common to all of them is a need to create a map from the original array, use it in…