欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript數(shù)字?jǐn)?shù)組的13個(gè)實(shí)用小技巧

 更新時(shí)間:2023年11月21日 09:48:51   作者:千鋒教育官方  
數(shù)組是JS最常見的一種數(shù)據(jù)結(jié)構(gòu),咱們在開發(fā)中也經(jīng)常用到,在這篇文章中,提供一些小技巧,幫助咱們提高開發(fā)效率,這篇文章主要給大家分享介紹了關(guān)于JavaScript數(shù)字?jǐn)?shù)組的13個(gè)實(shí)用小技巧,需要的朋友可以參考下

關(guān)于數(shù)字

1.進(jìn)制轉(zhuǎn)換

將十進(jìn)制數(shù)字轉(zhuǎn)換成其他N進(jìn)制的數(shù)字,可以使用toString(n)

const toDecimal = (num, n = 10) => num.(n) 
// 假設(shè)數(shù)字10要轉(zhuǎn)換成2進(jìn)制
toDecimal(10, 2) // '1010'

將n進(jìn)制數(shù)組轉(zhuǎn)換成十進(jìn)制,可以使用parseInt(num, n)

// 10的2進(jìn)制為1010
const toDecimalism = (num, n = 10) => parseInt(num, n)
toDecimalism(1010, 2)

2.四舍五入

當(dāng)你需要將小數(shù)點(diǎn)后的某些數(shù)字截?cái)?,并取四舍五?/p>

const toFixed = (n, fixed) => `${n}`.match(new RegExp(`^-?\d+(?:.\d{0,${fixed}})?`))[0]
toFixed(10.255, 2) // 10.25

3.補(bǔ)零

當(dāng)你需要在一個(gè)數(shù)字num不足len位數(shù)的時(shí)候前面補(bǔ)零操作

const replenishZero = (num, len, zero = 0) => num.().padStart(len, zero)
replenishZero(8, 2) // 08

4.數(shù)字正則

手機(jī)號格式化

當(dāng)你需要將手機(jī)號碼格式化成xxx-xxxx-xxxx的形式

const formatPhone = (str, sign = '-') => str.replace(/(\W|\s)/g, "").split(/^(\d{3})(\d{4})(\d{4})$/).filter(item => item).join(sign)

formatPhone('13123456789') // '131-2345-6789'
formatPhone('13 1234 56 789', ' ') // '131 2345 6789'

去除多余空格

當(dāng)你需要將一段文本中的多個(gè)空格合并成一個(gè)空格

const setTrimOut = str => str.replace(/\s\s+/g, ' ')
const str = setTrimOut('hello,   jack') // 

 關(guān)于數(shù)組

1.生成數(shù)組

當(dāng)你需要要生成一個(gè)0-99的數(shù)組

方案1

const createArr = (n) => Array.from(new Array(n), (v, i) => i)
const arr = createArr(100) // 0 - 99 數(shù)組

方案2

const createArr = (n) => new Array(n).fill(0).map((v, i) => i)
createArr(100) // 0 - 99數(shù)組

2.打亂數(shù)組

當(dāng)你有一個(gè)數(shù)組,你需要打亂這個(gè)數(shù)組的排序

const randomSort = list => list.sort(() => Math.random() - 0.5)
randomSort([0,1,2,3,4,5,6,7,8,9]) // 隨機(jī)排列結(jié)果

3.數(shù)組去重

當(dāng)你需要將數(shù)組中的所有重復(fù)的元素只保留一個(gè)

const removeDuplicates = list => [...new Set(list)]
removeDuplicates([0, 0, 2, 4, 5]) // [0,2,4,5]

4.多數(shù)組取交集

當(dāng)你需要取多個(gè)數(shù)組中的交集

const intersection = (a, ...arr) => [...new Set(a)].filter((v) => arr.every((b) => b.includes(v)))

intersection([1, 2, 3, 4], [2, 3, 4, 7, 8], [1, 3, 4, 9])
// [3, 4]

5.查找最大值索引

但你需要找到一個(gè)數(shù)組中的最大值的索引

const indexOfMax = (arr) => arr.reduce((prev, curr, i, a) => (curr > a[prev] ? i : prev), 0);
indexOfMax([1, 3, 9, 7, 5]); // 2

6.查找最小值索引

當(dāng)你需要找到一個(gè)數(shù)組中的最小值的索引

const indexOfMin = (arr) => arr.reduce((prev, curr, i, a) => (curr < a[prev] ? i : prev), 0)
indexOfMin([2, 5, 3, 4, 1, 0, 9]) // 5

7.找到最接近的數(shù)值

當(dāng)你需要在一個(gè)數(shù)組中找到一個(gè)最接近的值

const closest = (arr, n) => arr.reduce((prev, curr) => (Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev))
closest([29, 87, 8, 78, 97, 20, 75, 33, 24, 17], 50) // 33

8.壓縮多個(gè)數(shù)組

當(dāng)你需要將多個(gè)數(shù)組壓縮成一個(gè)數(shù)組

const zip = (...arr) => Array.from({ length: Math.max(...arr.map((a) => a.length)) }, (_, i) => arr.map((a) => a[i]))
zip([1,2,3,4], ['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'])
// [[1, 'a', 'A'], [2, 'b', 'B'], [3, 'c', 'C'], [4, 'd', 'D']]

9.矩陣交換行和列

當(dāng)你需要將一個(gè)矩陣的行和列進(jìn)行互相交換

const transpose = (matrix) => matrix[0].map((col, i) => matrix.map((row) => row[i]));
transpose(
    [              // [
        [1, 2, 3], //      [1, 4, 7],
        [4, 5, 6], //      [2, 5, 8],
        [7, 8, 9], //      [3, 6, 9],
     ]             //  ]
 ); 

總結(jié) 

到此這篇關(guān)于JavaScript數(shù)字?jǐn)?shù)組的13個(gè)實(shí)用小技巧的文章就介紹到這了,更多相關(guān)JS數(shù)字?jǐn)?shù)組技巧內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論