日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

六個(gè)你必須知道的 ES6 中很酷的數(shù)組函數(shù)

來(lái)源: 責(zé)編: 時(shí)間:2024-01-08 17:09:53 230觀看
導(dǎo)讀1. Array.of關(guān)于數(shù)組函數(shù),眾所周知,我們可以通過(guò)Array函數(shù)做以下事情。初始化指定長(zhǎng)度的數(shù)組設(shè)置數(shù)組的初始值// 1. Initialize an array of the specified lengthconst array1 = Array(3) // [ , , ]// 2. Set the ini

1. Array.of

aOk28資訊網(wǎng)——每日最新資訊28at.com

關(guān)于數(shù)組函數(shù),眾所周知,我們可以通過(guò)Array函數(shù)做以下事情。aOk28資訊網(wǎng)——每日最新資訊28at.com

  • 初始化指定長(zhǎng)度的數(shù)組
  • 設(shè)置數(shù)組的初始值
// 1. Initialize an array of the specified lengthconst array1 = Array(3) // [ , , ]// 2. Set the initial value of the arrayconst array2 = Array() // []const array3 = Array(undefined) // [ undefined ]const array4 = Array(1, 2, 3) // [ 1, 2, 3 ]

傳遞給Array函數(shù)的參數(shù)個(gè)數(shù)不一樣,其功能也不一樣。這常常讓我感到困惑。aOk28資訊網(wǎng)——每日最新資訊28at.com

幸運(yùn)的是,我們可以使用Array.of來(lái)彌補(bǔ)Array的缺點(diǎn)。aOk28資訊網(wǎng)——每日最新資訊28at.com

// it's not initializing an array of length 3const array1 = Array.of(3) // [ 3 ]const array2 = Array.of() // []const array3 = Array.of(undefined) // [ undefined ]const array4 = Array.of(1, 2, 3) // [ 1, 2, 3 ]

2. Array.from

在該方法中,我們可以通過(guò) Array.from 方法將類數(shù)組對(duì)象、arguments 對(duì)象和 NodeList 對(duì)象轉(zhuǎn)換為真正的數(shù)組。aOk28資訊網(wǎng)——每日最新資訊28at.com

類似數(shù)組的對(duì)象aOk28資訊網(wǎng)——每日最新資訊28at.com

const arrayLike = {  0: 'fatfish',  1: 'medium',  length: 2}const array1 = [].slice.call(arrayLike) // ['fatfish', 'medium']// A more convenient wayconst array2 = Array.from(arrayLike) // ['fatfish', 'medium']

1). 節(jié)點(diǎn)列表

const domsNodeList = document.querySelectorAll('div')const domsArray = Array.from(domsNodeList) // [ dom, dom, dom, ... ]

2). Arguments

const logInfo = function () {  console.log('arguments', arguments)  console.log('Array.from arguments', Array.from(arguments))}logInfo('fatfish', 100)logInfo('fatfish')

3).Array.from的第二個(gè)參數(shù)

我們可以使用 Array.from 方法,例如“[].map”。aOk28資訊網(wǎng)——每日最新資訊28at.com

const array = [ 1, 2, 3 ]const array2 = array.map((num) => num * 2) // [2, 4, 6]const array3 = Array.from(array, (num) => num * 2) // [2, 4, 6]

3. includes

我們經(jīng)常會(huì)寫(xiě)這樣的判斷語(yǔ)句,當(dāng)其中一個(gè)條件滿足時(shí)做某事。aOk28資訊網(wǎng)——每日最新資訊28at.com

const num = 1if (num === 1 || num === 2 || num === 3 || num === 4) {  console.log(num) // 1}

其實(shí)可以通過(guò)include方法來(lái)簡(jiǎn)化代碼。aOk28資訊網(wǎng)——每日最新資訊28at.com

const nums = [ 1, 2, 3, 4 ]const num = 1if (nums.includes(num)) {  console.log(num) // 1}

4.使用at方法讀取數(shù)組尾部元素

如何讀取數(shù)組的尾部元素?是的,我們需要以“array.length-1”作為下標(biāo)來(lái)讀取。aOk28資訊網(wǎng)——每日最新資訊28at.com

const array = [ 1, 2, 3, 4, 5 ]const lastEle = array[ array.length - 1 ] // 5// You can't read like thatconst lastEle = array[ - 1 ] // undefined

還有其他辦法嗎?aOk28資訊網(wǎng)——每日最新資訊28at.com

是的,“at”方法將是你的魔力。當(dāng)然,您可以讀取數(shù)組中其他位置的元素。aOk28資訊網(wǎng)——每日最新資訊28at.com

const array = [ 1, 2, 3, 4, 5 ]const lastEle = array.at(-1) // 5const ele1 = array.at(0) // 1

5. flat

來(lái)自 MDN:“flat() 方法創(chuàng)建一個(gè)新數(shù)組,其中所有子數(shù)組元素遞歸地連接到其中,直到指定的深度。”aOk28資訊網(wǎng)——每日最新資訊28at.com

const array = [ 1, [ 2, [ 3, [ 4, [ 5 ] ] ] ] ]// The default depth is 1const flat1 = array.flat() // [ 1, 2, [ 3, [ 4, [ 5 ] ] ] ]const flat2 = array.flat(2) // [ 1, 2, 3, [ 4, [ 5 ] ] ]const flatAll = array.flat(Infinity) // [ 1, 2, 3, 4, 5 ]

6.findIndex

來(lái)自 MDN:“findIndex() 方法返回?cái)?shù)組中滿足所提供的測(cè)試函數(shù)的第一個(gè)元素的索引。否則,返回-1,表示沒(méi)有元素通過(guò)測(cè)試。”aOk28資訊網(wǎng)——每日最新資訊28at.com

const array = [ -1, 0, 10, 10,  20, 100 ]const index1 = array.findIndex((num) => num < 0) // 0const index2 = array.findIndex((num) => num >= 10) // 2


aOk28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-58895-0.html六個(gè)你必須知道的 ES6 中很酷的數(shù)組函數(shù)

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 12 個(gè)動(dòng)態(tài) JavaScript 動(dòng)畫(huà)庫(kù)增強(qiáng)用戶體驗(yàn)

下一篇: 如何自己實(shí)現(xiàn)一個(gè)靜態(tài)代碼分析工具?

標(biāo)簽:
  • 熱門(mén)焦點(diǎn)
Top 主站蜘蛛池模板: 罗山县| 岫岩| 怀柔区| 日喀则市| 荆州市| 枝江市| 高唐县| 南江县| 建瓯市| 阿尔山市| 黄龙县| 荆门市| 泾阳县| 南川市| 鄂尔多斯市| 客服| 曲阜市| 正蓝旗| 池州市| 册亨县| 阿拉善盟| 陆川县| 水城县| 台南市| 鹿邑县| 娄底市| 西平县| 镇江市| 荆州市| 固阳县| 谢通门县| 威远县| 会昌县| 黄浦区| 莎车县| 平塘县| 青河县| 孟津县| 同德县| 津市市| 北海市|