In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Javascript in how to achieve array leveling, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
The first is the first method, recursive processing, the code is as follows:
Var arr = [1,2,3, [3,3,3, [5, 4, 5, 6, 6, 7, 8]]
[333, 4444]
]
Function product () {
/ / 1. Create an empty array
Var newarr = []
/ 2, and returns a function whose argument is the array to be flattened
Return function flatten (arr) {
/ / 3, loop the array, judge each item, and insert it into the newarr if you don't lose
/ / if it is an array, recursively call faltten and merge the result with newarr
For (var t of arr) {
If (! Array.isArray (t)) {
Newarr.push (t)
} else {
Newarr.concat (flatten (t))
}
}
Return newarr
}
}
Var flatten = product ()
Console.log (flatten (arr))
The execution result is:
The above method is relatively regular, and the code is explained in the comments. The following method applies some new features of the javascript language, as follows:
Var arr = [1,2,3, [3,3,3, [5, 4, 5, 6, 6, 7, 8]]
[333, 4444]
]
Function flatten (arr) {
Return arr.reduce (function (pre,cur) {
If (! Array.isArray (cur)) {
Return [... pre,cur]
} else {
Return [. Pre,...flatten (cur)]
}
}, [])
}
Console.log (flatten (arr))
The above code uses a new feature of ES6 to extend the cloud algorithm "...", "[... abc,...fff]" which is equivalent to abc.concat (fff), which is more intuitive and clear, and uses the reduce method. Reduce is a method of arrays in the javascript language.
When the array calls the recduce method, you can pass two arguments, the first of which is a callback function and the second of which is an initial value. The callback function needs to pass two parameters, the first parameter is the return value of each execution of the function, and the second parameter is the value of the array corresponding to the current index. The second parameter of reduce can be omitted. If omitted, the first call parameter of the callback function is the value of the first and second terms of the array. If it is not omitted, the first parameter of the callback function is this initial value. In the above example, the second parameter of reduce sets an empty array.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.