自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

七個(gè)我在工作中經(jīng)常使用的JavaScript技巧

開(kāi)發(fā) 前端
本文總結(jié)了我在工作中經(jīng)常使用的 7 個(gè) JavaScript 技巧,希望對(duì)你也有幫助。


前言

ES6 給我們編程帶來(lái)了很多便利,以前用大量代碼實(shí)現(xiàn)的功能現(xiàn)在變得非常簡(jiǎn)潔。

本文總結(jié)了我在工作中經(jīng)常使用的 7 個(gè) JavaScript 技巧,希望對(duì)你也有幫助。

一. 找出數(shù)組中的最大值或最小值

有時(shí),我們需要找到數(shù)組中的最大值,你通常是怎么做的?

解決方案 1

我們可以先對(duì)數(shù)組進(jìn)行排序,然后,數(shù)組的最后一項(xiàng)就是最大值。

const array = [ 1, 10, -19, 2, 7, 100 ]
array.sort((a, b) => a - b)
console.log('max value', array[ array.length - 1 ]) // 100
console.log('min value', array[ 0 ]) // -19

解決方案 2

還有其他解決方案嗎?是的,我們可以使用“Math.max”輕松處理它。

const array = [ 1, 10, -19, 2, 7, 100 ]
console.log('max value', Math.max(...array)) // 100
console.log('min value', Math.min(...array)) // -19

二. 計(jì)算數(shù)組的總和

如果有一個(gè)數(shù)字?jǐn)?shù)組,得到它們總和的最快方法是什么?

const array = [ 1, 10, -19, 2, 7, 100 ]
const sum = array.reduce((sum, num) => sum + num) // 101

三. 從數(shù)組中獲取隨機(jī)值

給你一個(gè)數(shù)組,現(xiàn)在你想從中獲取一個(gè)隨機(jī)值,你怎么做呢?

const array = [ 'fatfish', 'fish', 24, 'hello', 'world' ]
const getRandomValue = (array) => {
return array[ Math.floor(Math.random() * array.length) ]
}
console.log(getRandomValue()) // world
console.log(getRandomValue()) // world
console.log(getRandomValue()) // 24

四. 隨機(jī)打亂數(shù)組的值

我們?cè)诔楠?jiǎng)的時(shí)候,需要打亂抽獎(jiǎng)順序。

const prizes = [ '??', '??', '??', '??' ]
prizes.sort(() => 0.5 - Math.random())
console.log(prizes) // ['??', '??', '??', '??']
prizes.sort(() => 0.5 - Math.random())
console.log(prizes) // ['??', '??', '??', '??']

五. 展平多層陣列

現(xiàn)在我們有了一個(gè)多維嵌套數(shù)組,如何將其鋪成一維數(shù)組?

解決方案 1

const array = [ 1, [ 2, [ 3, [ 4, [ 5 ] ] ] ] ]
const flattenArray = (array) => {
return array.reduce((res, it) => {
return res.concat(Array.isArray(it) ? flattenArray(it) : it)
}, [])
}
console.log(flattenArray(array)) // [1, 2, 3, 4, 5]

解決方案 2

事實(shí)上,我們有一個(gè)更簡(jiǎn)單的方法來(lái)解決它。關(guān)于flat,我們來(lái)看看MDN的解釋?zhuān)?

flat() 方法創(chuàng)建一個(gè)新數(shù)組,其中所有子數(shù)組元素遞歸連接到指定深度。

const array = [ 1, [ 2, [ 3, [ 4, [ 5 ] ] ] ] ]
console.log(array.flat(Infinity)) // [1, 2, 3, 4, 5]

六. 檢查數(shù)組是否包含值

過(guò)去,我們總是使用“indexOf”方法來(lái)檢查數(shù)組是否包含值。如果“indexOf”返回的值大于-1,則表示有一個(gè)。

const array = [ 'fatfish', 'hello', 'world', 24 ]
console.log(array.indexOf('fatfish')) // 0
console.log(array.indexOf('medium')) // -1

但是,現(xiàn)在數(shù)據(jù)比較復(fù)雜,我們將無(wú)法通過(guò) indexOf 方法直接確認(rèn)數(shù)組中是否存在“fatfish”。幸運(yùn)的是,ES6 中提供了 findIndex 方法。

const array = [
{
name: 'fatfish
},
{
name: 'hello'
},
{
name: 'world'
},
]
const index = array.findIndex((it) => it.name === 'fatfish') // 0

七. 使用“includes”方法進(jìn)行判斷

你一定見(jiàn)過(guò)這樣的判斷方法,雖然,可以達(dá)到條件判斷的目的,但是,看起來(lái)很繁瑣。

const value = 'fatfish'

if (value === 'fatfish' || value === 'medium' || value === 'fe') {

console.log('hello world') // hello world

}

我們可以使用includes方法讓代碼更簡(jiǎn)單甚至更可擴(kuò)展。

const conditions = [ 'fatfish', 'medium', 'fe' ]
const value = 'fatfish'
if (conditions.includes(value)) {
console.log('hello world') // hello world
}
責(zé)任編輯:龐桂玉 來(lái)源: 大前端私房菜
相關(guān)推薦

2022-09-25 22:56:52

JavaScrip編程技巧

2020-12-07 09:56:34

GitLinux版本控制系統(tǒng)

2023-09-07 16:28:46

JavaScrip

2021-11-22 12:13:54

Linuxwget 命令

2021-06-28 11:46:31

GitLinux

2015-11-30 17:12:31

Git使用技巧

2019-03-06 14:26:31

Javascript面試前端

2019-12-11 15:21:12

PythonExcel瀏覽器

2023-03-19 16:02:33

JavaScrip技巧編程語(yǔ)言

2022-09-20 12:59:36

JavaScript函數(shù)

2021-11-03 06:57:41

Vue源碼應(yīng)用

2019-04-26 14:11:56

技巧工程師實(shí)踐

2019-08-07 16:50:38

SQLjoingroup

2021-06-07 14:36:58

iPadSiri辦公

2023-01-10 14:54:19

2024-11-27 08:28:37

2010-03-31 17:17:32

2023-05-30 09:59:38

2018-05-24 08:47:15

數(shù)據(jù)存儲(chǔ)技巧

2021-08-17 10:08:44

HTML網(wǎng)站網(wǎng)絡(luò)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)