做者:Ghazi Khan
译者:前端小智
来源:codewithghazi
点赞再看,微信搜索
【大迁世界】 关注这个没有大厂背景,但有着一股向上积极心态人。本文
GitHub
https://github.com/qq44924588... 上已经收录,文章的已分类,也整理了不少个人文档,和教程资料。
在前端开发中,数组是常常会被用到的数组结构,今天,介绍 5 个处理数组技巧,但愿能带给大家一些 启发和帮助。废话很少说,让咱们开始吧。javascript
在开发者,有时候咱们须要对数组的顺序进行从新的洗牌。 在 JS 中并无提供数组随机排序的方法,这里提供一个随机排序的方法:前端
function shuffle(arr) { var i, j, temp; for (i = arr.length - 1; i > 0; i--) { j = Math.floor(Math.random() * (i + 1)); temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } return arr; }
在开发者,咱们常常须要过滤重复的值,这里提供几种方式来过滤数组的重复值。java
使用 Set()
函数,此函数可与单个值数组一块儿使用。对于数组中嵌套的对象值而言,不是一个好的选择。git
const numArray = [1,2,3,4,2,3,4,5,1,1,2,3,3,4,5,6,7,8,2,4,6]; // 使用 Array.from 方法 Array.from(new Set(numArray)); // 使用展开方式 [...new Set(numArray)]
使用 filter
方法,咱们能够对元素是对象的进行过滤。github
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; } }) }
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 }) }
对象数组
进行排序咱们知道 JS 数组中的 sort 方法是按字典顺序进行排序的,因此对于字符串类, 该方法是能够很好的正常工做,但对于数据元素是对象类型,就不太好使了,这里咱们须要自定义一个排序方法。数组
在比较函数中,咱们将根据如下条件返回值:微信
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)
JS 中有个方法能够作到这一点,就是使用数组中的 .join()
方法,咱们能够传入指定的符号来作数组进行分隔。dom
const data = ['Mango', 'Apple', 'Banana', 'Peach'] data.join(','); // return "Mango,Apple,Banana,Peach"
对于此任务,咱们有多种方式,一种是使用 forEach
组合 if-else
的方式 ,另外一种可使用filter
方法,可是使用forEach
和filter
的缺点是:函数
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'}
人才们的 【三连】 就是小智不断分享的最大动力,若是本篇博客有任何错误和建议,欢迎人才们留言,最后,谢谢你们的观看。
代码部署后可能存在的BUG无法实时知道,过后为了解决这些BUG,花了大量的时间进行log 调试,这边顺便给你们推荐一个好用的BUG监控工具 Fundebug。
原文:https://codewithghazi.com/jav...
文章每周持续更新,能够微信搜索 【大迁世界 】 第一时间阅读,回复 【福利】 有多份前端视频等着你,本文 GitHub https://github.com/qq449245884/xiaozhi 已经收录,欢迎Star。