In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use JS array techniques to improve development skills". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Random arrangement
Among developers, sometimes we need to reshuffle the order of the array. The method of random sorting of arrays is not provided in JS. Here is a method of random sorting:
Function shuffle (arr) {var I, j, temp; for (I = arr.length-1; I > 0; iMel -) {j = Math.floor (Math.random () * (I + 1)); temp = arr [I]; arr [I] = arr [j]; arr [j] = temp;} return arr;}
two。 Unique value
Among developers, we often need to filter duplicate values, and here are several ways to filter duplicate values of arrays.
Working with Set objects
Use the Set () function, which can be used with a single array of values. It is not a good choice for object values nested in an array.
Const numArray = [1 new Set 2 (numArray)); / / use the Array.from method Array.from (new Set (numArray)); / / use the unfolding method [. New Set (numArray)]
Use Array.filter
Using the filter method, we can filter elements that are objects.
Const data = [{id: 1, name: 'Lemon'}, {id: 2, name:' Mint'}, {id: 3, name: 'Mango'}, {id: 4, name:' Apple'}, {id: 5, name: 'Lemon'}, {id: 6, name:' Mint'}, {id: 7, name: 'Mango'}, {id: 8, name:' Apple'} ] function findUnique (data) {return data.filter ((value, index, array) = > {if (array.findIndex (item = > item.name = value.name) = index) {return value })}
3. Using the lodash method of loadsh
Import {uniqBy} from 'lodash' const data = [{id: 1, name:' Lemon'}, {id: 2, name: 'Mint'}, {id: 3, name:' Mango'}, {id: 4, name: 'Apple'}, {id: 5, name:' Lemon'}, {id: 6, name: 'Mint'}, {id: 7, name:' Mango'}, {id: 8 Name: 'Apple'},] function findUnique (data) {return uniqBy (data, e = > {return e.name})}
3. Sort an array of objects by attribute
We know that the sort method in the JS array is sorted in dictionary order, so this method works well for string classes, but not for data elements that are object types, so we need to customize a sorting method here.
In the comparison function, we will return the value based on the following conditions:
Less than 0A before B
Greater than 0: B before A
Equal to 0: an and B remain the same to each other.
Const data = [{id: 1, name: 'Lemon', type:' fruit'}, {id: 2, name: 'Mint', type:' vegetable'}, {id: 3, name: 'Mango', type:' grain'}, {id: 4, name: 'Apple', type:' fruit'}, {id: 5, name: 'Lemon', type:' vegetable'}, {id: 6, name: 'Mint' Type: 'fruit'}, {id: 7, name:' Mango', type: 'fruit'}, {id: 8, name:' Apple', type: 'grain'},] function compare (a, b) {/ / Use toLowerCase () to ignore character casing const typeA = a.type.toLowerCase () Const typeB = b.type.toLowerCase (); let comparison = 0; if (typeA > typeB) {comparison = 1;} else if (typeA)
< typeB) { comparison = -1; } return comparison; } data.sort(compare) 4. 把数组转成以指定符号分隔的字符串 JS 中有个方法可以做到这一点,就是使用数组中的 .join() 方法,我们可以传入指定的符号来做数组进行分隔。 const data = ['Mango', 'Apple', 'Banana', 'Peach'] data.join(','); // return "Mango,Apple,Banana,Peach" 5. 从数组中选择一个元素 对于此任务,我们有多种方式,一种是使用 forEach 组合 if-else 的方式 ,另一种可以使用filter 方法,但是使用forEach 和filter的缺点是: 鸿蒙官方战略合作共建--HarmonyOS技术社区 在forEach中,我们要额外的遍历其它不需要元素,并且还要使用 if 语句来提取所需的值。 在filter 方法中,我们有一个简单的比较操作,但是它将返回的是一个数组,而是我们想要是根据给定条件从数组中获得单个对象。 为了解决这个问题,我们可以使用 find函数从数组中找到确切的元素并返回该对象,这里我们不需要使用if-else语句来检查元素是否满足条件。 const data = [ {id: 1, name: 'Lemon'}, {id: 2, name: 'Mint'}, {id: 3, name: 'Mango'}, {id: 4, name: 'Apple'} ] const value = data.find(item =>Item.name = = 'Apple') / / value = {id: 4, name:' Apple'} "this is the end of the introduction to" how to improve your development skills with JS array techniques ". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.