In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the methods of solving array deduplication and array flattening". In daily operation, I believe many people have doubts about the methods of solving array deduplication and array flattening. I have consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "solving array de-duplication and array flattening methods". Next, please follow the editor to study!
Array deduplication
Array deemphasis is a basic question in the interview, but you can still mine a lot of js knowledge in it.
Set weight removal is most commonly used in ES6.
A Set object is a collection of values, and you can iterate over its elements in the order in which they are inserted. Elements in Set appear only once, that is, elements in Set are unique.
Function unique (arr) {return Array.from (new Set (arr))} let arr = [1,1, 'true',' true', true, true, 15,15]; console.log (unique (arr)) / / [1, 'true', true, 15] let unique2 = arr = > [... new Set (arr)] console.log (unique2 (arr)); / / [1,' true', true, 15]
Double for cycle
The outer loop element, and the inner loop compares the value.
Function unique (arr) {for (var I = 0; I)
< arr.length; i++) { for (var j = i + 1; j < arr.length; j++) { if (arr[i] == arr[j]) { //第一个等同于第二个,splice方法删除第二个 arr.splice(j, 1); j--; } } } return arr; } let arr = [1, 1, 'true', 'true', true, true, 15, 15]; console.log(unique(arr))// [ 1, 'true', true, 15 ] indexOf或includes去重 当数组内没有该元素时,indexOf返回-1,则把它push进新数组。 function unique(arr) { var array = []; for (var i = 0; i < arr.length; i++) { if (array .indexOf(arr[i]) === -1) { array .push(arr[i]) } } return array; } includes() 方法用于判断字符串是否包含指定的子字符串。如果找到匹配的字符串则返回 true,否则返回 false。 function unique(arr) { var array = []; for (var i = 0; i < arr.length; i++) { if (!array.includes( arr[i]) ) { array .push(arr[i]) } } return array; } filter去重 原始数组中元素的索引等于当前索引值时返回,否则返回当前元素 function unique(arr) { var res = arr.filter(function(item, index, array) { return array.indexOf(item) === index }) return res }数组扁平化 数组扁平化是指将一个多维数组变为一维数组。 flat方法 flat()方法创建一个新数组,其中所有子数组元素都以递归方式连接到该数组中,直到达到指定的深度为止 const arr1 = [1, 2, [3, 4, [5, 6]]]; arr1.flat(2); // [1, 2, 3, 4, 5, 6] const arr2 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]; arr2.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ES6 拓展运算符 es6的扩展运算符能将二维数组变为一维,若arr中含有数组则使用一次扩展运算符,直至没有为止。 function flatten(arr) { while (arr.some(item =>Array.isArray (item)) {arr = [] .concat (.arr);} return arr;}
ToString method + split method
Call the toString method of the array, turn the array into a string and then use split to split it back to the array. Each item in the split split array is a string, so you need to use a map method to traverse the array to convert each item to a numeric value.
Function flatten (arr) {console.log (arr.toString ()); / 1 split (',') .map (function (item) {return Number (item)) })} let ary = [1, [2, [3, [4,5], 6]; console.log (flatten (ary)); / / [1,2,3,4,5,6]
You can use the join method to achieve an effect similar to the toString method.
Recursion
Iterate through each item recursively, continuing when it is an array, or concat if it is not an array.
Function flatten (arr) {var result = []; for (var I = 0, len = arr.length; I)
< len; i++) { if (Array.isArray(arr[i])) { result = result.concat(flatten(arr[i])) } else { result.push(arr[i]) } } return result; } reduce方法实现 reduce方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。 function flatten(arr) { return arr.reduce((result, item)=>{return result.concat (Array.isArray (item)? Flatten (item): item);}, []);} let ary = [1, [2, [3, [4, 5], 6]; console.log (flatten (ary)); / / [1, 2, 3, 4, 5, 6] at this point, the study of "what are the methods of array de-duplication and array flattening" is over, hoping to solve everyone's 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.