In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge points about the method of removing weight from javascript array. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
The first function: array deduplication.
Implementation scenario 1:
Idea: do for loop traversal on the deduplicated array arr, and then find the element arr [I] that is currently traversed in the new array newArr. If the return value is-1, it means that the element arr [I] that is currently traversed has not appeared in the new array, then put the element into the new array. When the array arr is traversed, the new array newArr is the array after it is deduplicated.
Var arr = [1,4,7,4,3,2,1,4,7]
Var newArr = []
For (var I = 0; I
< arr.length; i++) { if(newArr.indexOf(arr[i]) === -1) { newArr.push(arr[i]); } } console.log(newArr); 实现方案2: 思路:该方案和方案1思路相同,只不过是使用reduce方法进行遍历。 var arr = [1, 4, 7, 4, 3, 2, 1, 4, 7]; var newArr = arr.reduce((newArr, current) =>{
If (newArr.indexOf (current) = =-1) {
NewArr.push (current)
}
Return newArr
}, [])
Console.log (newArr)
Implementation scenario 3:
Idea: the current scheme uses the deduplication feature of set data deconstruction added by ES6, and then converts the generated set objects into an array.
Var arr = [1,4,7,4,3,2,1,4,7]
Var newArr = Array.from (new Set (arr))
Console.log (newArr)
The second function: find the minimum (or maximum) value in the array.
Implementation scenario 1:
Idea: assume that the first array element is the minimum, and then traverse and judge the elements behind the array. If the element currently being traversed is less than the value of the 伊明 variable, it means that the value of the 伊明 variable is not the minimum. Replace the value of the 伊明 variable with the value of the element currently traversed. When the loop ends, the value of the 伊明 variable is the smallest value in the array.
Var arr = [23, 45, 40, 30, 12]
Var 伊明 = arr.shift ()
Arr.forEach ((v) = > {
伊明 = v < 伊明? V: 伊明
})
Console.log (伊明)
Implementation scenario 2:
Idea: use the Math.min () method to minimize the value, but the parameter of this method is a numerical list, not an array, so use the new extension operator of ES6 to convert the array into a list, and then pass it to the Math.min () method.
Var arr = [23, 45, 40, 30, 12]
Var 伊明 = Math.min (. Arr)
Console.log (伊明)
The third function: exchange the values of two variables.
Implementation scenario 1:
Idea: give the value of variable a to the temporary variable temp, then the value of variable b to variable a, and finally the value of temporary variable temp to variable b, thus completing the function of exchanging values between variable an and variable b.
Var a = 4, b = 6
Var temp = a
A = b
B = temp
Console.log (a, b)
Implementation scenario 2:
Idea: using the array deconstruction method of ES6 to complete the data exchange between variable an and variable b without using the third variable.
Var a = 4, b = 6
[a, b] = [b, a]
Console.log (a, b)
These are all the contents of this article entitled "how to remove duplicates from javascript arrays". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.