What do we need to know when we use a thing? Why---where----how to---when. What do we use for a thing? Where do you use it? How to use it? When to use? Instead of passively accepting something. Use in JS inside I think also will try. has been the pursuit of source JS, although also have a lot of things back, but with the passage of time, the busy work is forgotten, and sometimes even a little impression, which let me start thinking about my way of learning has been thinking. What's the use of remembering a word that is not simple and then starting to associate it with the word? Must be something I'm going to do? Then associate to who can do it. Learning with purpose.
Now there are 10 people (that is, arrays) that need us to jump out of a tiger. What should we do? Must be one to ask. Do you belong to the tiger? He's going to answer yes. Then pull aside. If not, ignore continue next. The end result is a queue (array) of people of the Tiger Group. Then the filter in JS is a similar function to do. Determines the elements in an array that satisfy certain conditions and returns an array of these elements.
To take a look at an example, there is a data
1 vardata = [2 {3Id:1,4Name: ' A ',5 children:[6 {7Id:2,8Name: ' B ',9 children:[Ten { OneId:3, AName: ' C ' - } - ] the } - ] - }
]
Now let me get the data with ID 3. What do you do? This problem involves a recursive problem. What do you mean, recursion? is to meet certain conditions to do the repetitive action.
So let's write a function to implement this function.
function GetItem (data,id) {let result; = Data.filter (function (item) { return item.id = = = ID }) if (data.children) { = result.concat (GetItem (Data.children,id)) } return result}
1: When we recursive, must be this item contains children time. So what's the repetitive action? is to iterate over the array, one by one to determine if the ID is 3. So we have finished this function, of course, this function is not perfect, no data type restrictions and so on. Probably not well written ... But I hope the idea can be used a little.
The filter function in the array, recursion, and some applications.