In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to remove the weight of the array in JavaScript". In the daily operation, I believe that many people have doubts about how to remove the weight of the array in JavaScript. The editor consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "how to remove the weight of array in JavaScript". Next, please follow the editor to study!
There are five methods of array deduplication: 1, using "[... new Set (arr)]" statement; 2, using "Array.from (new Set (arr))" statement to remove weight; 3, using indexOf () to remove weight; 4, using includes () to remove weight; 5, using filter () to remove weight.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
The method of removing the weight of array
1. [... new Set (arr)]
Const arr = [1, 2, 3, 2, 3]; [... new Set (arr)]; / / [1,2,3]
Here, the Set object is converted into an array through ES6's expansion syntax
2. Array.from (new Set (arr))
Const arr = [1,2,3,2,3]; Array.from (new Set (arr)); / / [1,2,3]
Because elements in Set are unique, whether they are original values or object references, you can de-duplicate by converting an array to a Set object
The Array.from method converts a Set object to an array
3. Use indexOf to remove weight
Function unique (arr) {if (! Array.isArray (arr)) {console.log ('type errorships') Return} var array = []; for (var I = 0; I < arr.length; iTunes +) {if (array .indexOf (arr [I]) =-1) {array .push (arr [I])} return array } var arr = [1pr 1rec NaN,'NaN', null,null, NaN, NaN,'NaN', 0,0, 'averse,' averse, {}, {}]; console.log (unique (arr)) / / [1, "true", true,15, false, undefined, null, NaN, NaN, "NaN", 0, "a", {… }, {... }] / / NaN, {} have not been duplicated
Create an empty result array, for loop the original array, determine whether there is a current element in the result array, skip if it has the same value, and push into the array if it is different.
4. Make use of includes
Function unique (arr) {if (! Array.isArray (arr)) {console.log ('type errorships') Return} var array = []; for (var I = 0; I < arr.length; iTunes +) {if (! array.includes (arr [I])) {/ / includes detects whether the array has a value array.push (ARR [I]) }} return array} var arr = [1pc1, null,null, NaN, NaN,'NaN', 0,0, 'averse,' averse, {}, {}]; console.log (unique (arr)) / / [1, "true", true,15, false, undefined, null, NaN, "NaN", 0, "a", {… }, {... }] / / {} did not remove the weight
5. Make use of filter
Function unique (arr) {return arr.filter (function (item, index, arr) {/ / current element, first index in the original array = = current index value, otherwise return current element return arr.indexOf (item, 0) = index;}) } var arr = [1 unique (arr)) / / [1, true, true,15, false, undefined, null, NaN ", 0," a ", {… }, {... }] at this point, the study of "how to duplicate arrays in JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.