色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

JavaScript的一些小技巧分享

瀏覽:99日期:2023-10-05 15:49:03
數(shù)組去重

ES6提供了幾種簡潔的數(shù)組去重的方法,但該方法并不適合處理非基本類型的數(shù)組。對于基本類型的數(shù)組去重,可以使用... new Set()來過濾掉數(shù)組中重復(fù)的值,創(chuàng)建一個只有唯一值的新數(shù)組。

const array = [1, 1, 2, 3, 5, 5, 1] const uniqueArray = [...new Set(array)]; console.log(uniqueArray); > Result:(4) [1, 2, 3, 5]

這是ES6中的新特性,在ES6之前,要實現(xiàn)同樣的效果,我們需要使用更多的代碼。該技巧適用于包含基本類型的數(shù)組:undefined、null、boolean、string和number。如果數(shù)組中包含了一個object,function或其他數(shù)組,那就需要使用另一種方法。

除了上面的方法之外,還可以使用Array.from(new Set())來實現(xiàn):

const array = [1, 1, 2, 3, 5, 5, 1] Array.from(new Set(array)) > Result:(4) [1, 2, 3, 5]

另外,還可以使用Array的.filter及indexOf()來實現(xiàn):

const array = [1, 1, 2, 3, 5, 5, 1] array.filter((arr, index) => array.indexOf(arr) === index) > Result:(4) [1, 2, 3, 5]

注意,indexOf()方法將返回數(shù)組中第一個出現(xiàn)的數(shù)組項。這就是為什么我們可以在每次迭代中將indexOf()方法返回的索引與當(dāng)索索引進(jìn)行比較,以確定當(dāng)前項是否重復(fù)。

確保數(shù)組的長度

在處理網(wǎng)格結(jié)構(gòu)時,如果原始數(shù)據(jù)每行的長度不相等,就需要重新創(chuàng)建該數(shù)據(jù)。為了確保每行的數(shù)據(jù)長度相等,可以使用Array.fill來處理

let array = Array(5).fill(’’); console.log(array); > Result: (5) ['', '', '', '', '']數(shù)組映射

不使用Array.map來映射數(shù)組值的方法。

const array = [ { name: ’大漠’, email: ’w3cplus@hotmail.com’ }, { name: ’Airen’, email: ’airen@gmail.com’ }] const name = Array.from(array, ({ name }) => name) > Result: (2) ['大漠', 'Airen']數(shù)組截斷

如果你想從數(shù)組末尾刪除值(刪除數(shù)組中的最后一項),有比使用splice()更快的替代方法。

例如,你知道原始數(shù)組的大小,可以重新定義數(shù)組的length屬性的值,就可以實現(xiàn)從數(shù)組末尾刪除值:

let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] console.log(array.length) > Result: 10 array.length = 4 console.log(array) > Result: (4) [0, 1, 2, 3]

這是一個特別簡潔的解決方案。但是,slice()方法運(yùn)行更快,性能更好:

let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; array = array.slice(0, 4); console.log(array); > Result: [0, 1, 2, 3]過濾掉數(shù)組中的falsy值

如果你想過濾數(shù)組中的falsy值,比如0、undefined、null、false,那么可以通過map和filter方法實現(xiàn):

const array = [0, 1, ’0’, ’1’, ’大漠’, ’w3cplus.com’, undefined, true, false, null, ’undefined’, ’null’, NaN, ’NaN’, ’1’ + 0] array.map(item => { return item }).filter(Boolean) > Result: (10) [1, '0', '1', '大漠', 'w3cplus.com', true, 'undefined', 'null', 'NaN', '10']獲取數(shù)組的最后一項

數(shù)組的slice()取值為正值時,從數(shù)組的開始處截取數(shù)組的項,如果取值為負(fù)整數(shù)時,可以從數(shù)組末屬開始獲取數(shù)組項。

let array = [1, 2, 3, 4, 5, 6, 7] const firstArrayVal = array.slice(0, 1) > Result: [1] const lastArrayVal = array.slice(-1) > Result: [7] console.log(array.slice(1)) > Result: (6) [2, 3, 4, 5, 6, 7] console.log(array.slice(array.length)) > Result: []

正如上面示例所示,使用array.slice(-1)獲取數(shù)組的最后一項,除此之外還可以使用下面的方式來獲取數(shù)組的最后一項:

console.log(array.slice(array.length - 1)) > Result: [7]從數(shù)組中獲取最大值和最小值

可以使用Math.max和Math.min取出數(shù)組中的最大小值和最小值:

const numbers = [15, 80, -9, 90, -99] const maxInNumbers = Math.max.apply(Math, numbers) const minInNumbers = Math.min.apply(Math, numbers) console.log(maxInNumbers) > Result: 90 console.log(minInNumbers) > Result: -99

另外還可以使用ES6的...運(yùn)算符來完成:

const numbers = [1, 2, 3, 4]; Math.max(...numbers) > Result: 4 Math.min(...numbers) > Result: 1

以上就是JavaScript的一些小技巧分享的詳細(xì)內(nèi)容,更多關(guān)于JavaScript 小技巧的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 一区二区三区在线观看免费 | 成人在线免费观看网站 | 国产高清成人 | 日本成人免费在线观看 | 亚洲天堂免费看 | 国产一区二区免费在线观看 | 影院成人区精品一区二区婷婷丽春院影视 | 国产欧美日韩一区二区三区 | 欧美性一区二区三区 | 成人做爰在线视频 | 久久精品国产只有精品6 | 在线看片 在线播放 | 成人中文字幕在线高清 | 久久精品国产亚洲网址 | 大伊香蕉精品视频在线天堂 | 欧美一级片免费在线观看 | 亚洲永久中文字幕在线 | 日本午夜三级 | 国产日产欧产精品精品推荐小说 | 欧美三级一级片 | 在线观看国产精品日本不卡网 | 免费看a| 精品综合 | 免费永久观看美女视频网站网址 | 亚洲综合伊人色一区 | 日韩毛片欧美一级国产毛片 | 欧美一级毛片不卡免费观看 | 在线观看欧洲成人免费视频 | 亚洲欧美在线观看 | 国产精品久久久久久久久久久久久久 | 91久久精品国产91性色tv | 日韩欧美精品在线视频 | 国产不卡在线观看视频 | 欧美一级高清视频在线播放 | 国产精品久久久一区二区三区 | 91大神在线精品视频一区 | 欧美日韩在线永久免费播放 | 400部大量精品情侣网站 | 日韩精品亚洲专区在线观看 | 国产欧美亚洲精品a | 久久久久亚洲精品中文字幕 |