1. 獲取指定范圍內(nèi)的隨機(jī)整數(shù):
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
2. 打亂數(shù)組順序
let arr:[31,2,3,'排序','??']
arr = arr.sort(() => 0.5 - Math.random())
// [ 3, 2, "??", "排序", 31 ]
3. 用Math.max()取數(shù)組最大值
let arrr = [ 6, 1, 2, 8, 10 ]
// ES5 的寫(xiě)法
let max = Math.max.apply(Math,arr)
Math.max.apply(null, arrr)
// ES6使用拓展運(yùn)算符的寫(xiě)法
Math.max(...arr)
// max = 10
// 等同于
Math.max(14, 3, 77);
3. 去除數(shù)字之外的所有字符
var str = ''yannnianjunzi,,溫其如玉,,一個(gè)億,1000000000??"
let num = str.replace(/\D/g,'')
// num 1000000000
3.1 replaceAll方法
const str = "hello world";
// 之前
str.replace(/o/g, "a")// "hella warld"
// 現(xiàn)在
str.replaceAll("o", "a")// "hella warld"
4. 反轉(zhuǎn)字符串或者單詞
let str = 'dfvgwsdfgg,,奮斗奮斗參考看看,,dsfgf'
let reverseStr = str.split('').reverse().join('')
5. 將十進(jìn)制轉(zhuǎn)換為二進(jìn)制或十六進(jìn)制
let num = 12;
let num1 = Number(this.num10).toString(2)
let num2 = Number(this.num10).toString(16)
6. 如何檢查對(duì)象中是否存在某個(gè)屬性
6.1 第一種使用 in 操作符號(hào):
const o = {
"prop" : "bwahahah",
"prop2" : "hweasa"
};
console.log("prop" in o); // true
console.log("prop1" in o); // false
6.2 使用 hasOwnProperty 方法,,hasOwnProperty() 方法會(huì)返回一個(gè)布爾值,指示對(duì)象自身屬性中是否具有指定的屬性(也就是,,是否有指定的鍵)
6.2.1 Object.hasOwn()和Object.hasOwnProperty區(qū)別 JavaScript 對(duì)象的屬性分成兩種:自身的屬性和繼承的屬性,。對(duì)象實(shí)例有一個(gè)hasOwnProperty() 方法,可以判斷某個(gè)屬性是否為原生屬性,。ES2022 在Object 對(duì)象上面新增了一個(gè)靜態(tài)方法Object.hasOwn() ,,也可以判斷是否為自身的屬性。Object.hasOwn() 可以接受兩個(gè)參數(shù),,第一個(gè)是所要判斷的對(duì)象,,第二個(gè)是屬性名。
const foo = Object.create({ a: 123 });
foo.b = 456;
Object.hasOwn(foo, 'a') // false
Object.hasOwn(foo, 'b') // true
上面示例中,,對(duì)象foo的屬性a是繼承屬性,,屬性b是原生屬性。Object.hasOwn()對(duì)屬性a返回false,,對(duì)屬性b返回true,。
Object.hasOwn()的一個(gè)好處是,對(duì)于不繼承Object.prototype的對(duì)象不會(huì)報(bào)錯(cuò),,而hasOwnProperty()是會(huì)報(bào)錯(cuò)的,。
console.log(o.hasOwnProperty("prop2")); // true
console.log(o.hasOwnProperty("prop1")); // false
const obj = Object.create(null);
obj.hasOwnProperty('foo') // 報(bào)錯(cuò)
Object.hasOwn(obj, 'foo') // false
// 上面示例中,Object.create(null)返回的對(duì)象obj是沒(méi)有原型的,,不繼承任何屬性,,
// 這導(dǎo)致調(diào)用obj.hasOwnProperty()會(huì)報(bào)錯(cuò),但是Object.hasOwn()就能正確處理這種情況,。
6.3 第三種使用括號(hào)符號(hào)obj["prop"],。如果屬性存在,它將返回該屬性的值,,否則將返回undefined,。
console.log(o["prop"]); // "bwahahah"
console.log(o["prop1"]); // undefined
7.生成一個(gè)長(zhǎng)度為100并且內(nèi)容為0的數(shù)組
1. new Array(100).fill(0); Array(100).fill(0)
2. Array.from({length:100}, x => x=0)
3. Array.apply(null, {length:100} ).map(x => x=0)
Array.from()還可以接受一個(gè)函數(shù)作為第二個(gè)參數(shù),作用類似于數(shù)組的map()方法,,用來(lái)對(duì)每個(gè)元素進(jìn)行處理,,將處理后的值放入返回的數(shù)組。
Array.from(arrayLike, x => x * x);
// 等同于
Array.from(arrayLike).map(x => x * x);
Array.from([1, 2, 3], (x) => x * x)
// [1, 4, 9]
8. 通過(guò)toString來(lái)檢測(cè)各種數(shù)據(jù)類型
/**
* @description: 數(shù)據(jù)類型的檢測(cè)
* @param {any} data 要檢測(cè)數(shù)據(jù)類型的變量
* @return {string} type 返回具體的類型名稱【小寫(xiě)】
*/
const isTypeOf = (data) => {
return Object.prototype.toString.call(data).replace(/\[object (\w+)\]/, '$1').toLowerCase()
}
console.log(isTypeOf({})) // object
console.log(isTypeOf([])) // array
console.log(isTypeOf("ss")) // string
console.log(isTypeOf(1)) // number
console.log(isTypeOf(false)) // boolean
console.log(isTypeOf(/w+/)) // regexp
console.log(isTypeOf(null)) // null
console.log(isTypeOf(undefined)) // undefined
console.log(isTypeOf(Symbol("id"))) // symbol
console.log(isTypeOf(() => { })) // function
**9.遞歸求數(shù)組的和
getRecursionList(list){
function getSum(i) {
return i >= list.length ? 0 : list[i] + getSum(i+1)
}
// return getSum(0) // getSum(0) 相當(dāng)于調(diào)用了這個(gè)函數(shù)
return getSum(0)
}
|