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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail the "node array to remove the weight of what are the methods", the content is detailed, the steps are clear, the details are handled properly, I hope that this "node array to remove the weight of what are the methods" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
Methods: 1, using the map data structure to achieve de-duplication, each element of the array is stored in map as key; 2, using the set method in es6 to achieve de-duplication, the syntax is "Set (array)"; 3, using forEach statement with indexOf to achieve de-duplication.
This article operating environment: Windows10 system, nodejs version 12.19.0, Dell G3 computer.
Three methods of removing duplicates in node Array
The first one uses Map data structure to remove duplicates.
Create an empty Map data structure, iterate through the array that needs to be deduplicated, and store each element of the array in Map as key. Since the same key value will not appear in Map, the final result is the de-duplicated result.
Function a (arr) {let map = new Map (); let array = new Array (); / / the array is used to return the result for (let I = 0; I < arr.length If +) {if (map.has (arr [I])) {/ / if the key value map.set (arr [I], true);} else {map.set (arr [I], false) / / if there is no such key value array.push (arr [I]);}} return array;} var arr = [1, 1, 2, 2, 3, 3, 4, 4, 5]; console.log (a (arr)) / / [1, 2, 3, 4, 4, 5]
Second, using set in ES6 is the easiest way to remove duplicates.
Let arr2 = [1,2,3,4,5,5,4,3]; let res = [... new Set (arr2)]; console.log (res)
The third kind of forEach+indexOf implements de-duplication.
Var arr3 = [1,1,2,2,3,3,4,4,5,5]; var b = distinct (arr3); function distinct (arr) {result = [], len= arr3.length; / / len=10; arr3.forEach (function (v, I, arr3) {var bool = arr3.indexOf (v, I + 1) / / start looking for duplicate if (bool = =-1) {result.push (v);}} return result;} from the next index value of the passed parameter; console.log (b.toString ()) Here, the article "what are the ways to remove the repetition of node array" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow 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.