亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

JavaScript的一些小技巧分享

瀏覽:81日期:2023-10-05 15:49:03
數組去重

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

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之前,要實現同樣的效果,我們需要使用更多的代碼。該技巧適用于包含基本類型的數組:undefined、null、boolean、string和number。如果數組中包含了一個object,function或其他數組,那就需要使用另一種方法。

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

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

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

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

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

確保數組的長度

在處理網格結構時,如果原始數據每行的長度不相等,就需要重新創建該數據。為了確保每行的數據長度相等,可以使用Array.fill來處理

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

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

const array = [ { name: ’大漠’, email: ’[email protected]’ }, { name: ’Airen’, email: ’[email protected]’ }] const name = Array.from(array, ({ name }) => name) > Result: (2) ['大漠', 'Airen']數組截斷

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

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

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()方法運行更快,性能更好:

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]過濾掉數組中的falsy值

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

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']獲取數組的最后一項

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

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)獲取數組的最后一項,除此之外還可以使用下面的方式來獲取數組的最后一項:

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

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

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的...運算符來完成:

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

以上就是JavaScript的一些小技巧分享的詳細內容,更多關于JavaScript 小技巧的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 久久国产在线视频 | 日韩高清一区二区 | 中文字幕第一页在线视频 | 性欲影院 | 特别毛片| 日本乱人伦毛片 | 国产综合成人观看在线 | 91在线视频免费观看 | 欧美禁片在线观看免费 | 泰国午夜理伦三级在线观看 | 亚洲国产日韩无在线播放 | 不卡国产| 欧美人与鲁交大毛片免费 | 久久88香港三级 | 国产一区在线观看视频 | 黄色aⅴ| 亚洲国产精品一区二区三区 | 久久久久久88色愉愉 | 精品国产一区二区三区不卡 | 麻豆国产精品va在线观看不卡 | 国产3页| 亚洲高清日韩精品第一区 | 91免费国产高清在线 | 成人做爰免视频高清 | 欧美国产在线观看 | 在线免费观看污片 | 免费精品视频在线 | 一区二区视频在线观看高清视频在线 | 黄色在线观看免费 | 国产精品视频无圣光一区 | 一级毛片看真人在线视频 | 老黄网站在线观看免费 | 国产美女a做受大片在线观看 | 青青伊人91久久福利精品 | 在线观看精品视频 | 高清视频欧美性生活 | 亚洲精品成人一区 | 成年视频xxxxx在线入口 | 国模大胆偷拍在线视频 | 国产亚洲精品久久久久久 | 欧美xxxx69tube8护士|