日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue中向数组去重_「前端剑指offer第3期」来,手写一下数组去重

發(fā)布時(shí)間:2024/9/19 vue 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue中向数组去重_「前端剑指offer第3期」来,手写一下数组去重 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)組去重一般來說就這么幾種方法,理解代碼,記住就好!Map、Set、reduce、filter~

方法1 、Map

創(chuàng)建一個(gè)Map對(duì)象,把數(shù)組的值作為Map對(duì)象的索引,再獲取所有的索引。

const arr = [3, 1, 1, 2, 5, 9, 3, 0] const deduplication1 = arr => {let map = new Map()arr.forEach(v => map.set(v, 1))return [...map.keys()] }; console.log(deduplication1(arr));

方法2、Set

const arr = [3, 1, 1, 2, 5, 9, 3, 0] const deduplication2 = arr => [...new Set(arr)]; console.log(deduplication2(arr))

方法3、 reduce

const arr = [3, 1, 1, 2, 5, 9, 3, 0] const deduplication3 = arr => arr.reduce((temp, v) => temp.includes(v) ? temp : [...temp, v], []) console.log(deduplication3(arr))

方法4、filter

const arr = [3, 1, 1, 2, 5, 9, 3, 0] const deduplication4 = arr => arr.filter((val, index) => arr.indexOf(val) === index) console.log(deduplication4(arr))

方法5、排序去重

先排序,然后在再進(jìn)行過濾去掉相鄰相同的元素。這種方法不推薦。

const arr = [3, 1, 1, 2, 5, 9, 3, 0] const deduplication5 = arr => arr.sort().filter((val, index) => val !==arr[index+1]) console.log(deduplication5(arr))

作者 @饑人谷若愚

「前端劍指offer第1期」CSS選擇器優(yōu)先級(jí)是怎樣計(jì)算的?

「前端劍指offer第2期」一側(cè)定寬、一側(cè)自適應(yīng),盡量多的方案實(shí)現(xiàn)?

總結(jié)

以上是生活随笔為你收集整理的vue中向数组去重_「前端剑指offer第3期」来,手写一下数组去重的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。