AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

十個可以手動編寫的 JavaScript 數組 API

來源: 責編: 時間:2023-08-05 11:44:30 4933觀看
導讀JavaScript 中有很多API,使用得當,會很方便,省力不少。 你知道它的原理嗎? 今天這篇文章,我們將對它們進行一次小總結。現在開始吧。1.forEach()forEach()用于遍歷數組接收一參數回調函數,并在回調函數中接收三個參數,分別

JavaScript 中有很多API,使用得當,會很方便,省力不少。 你知道它的原理嗎? 今天這篇文章,我們將對它們進行一次小總結。COF28資訊網——每日最新資訊28at.com

COF28資訊網——每日最新資訊28at.com

現在開始吧。COF28資訊網——每日最新資訊28at.com

1.forEach()

forEach()用于遍歷數組接收一參數回調函數,并在回調函數中接收三個參數,分別代表每一項的值、下標和數組本身。COF28資訊網——每日最新資訊28at.com

為了確保數組可以訪問我們自己手寫的API,它必須鏈接到數組的原型。COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

const arr = [ { name: 'zt', age: 18 }, { name: 'aa', age: 19 }, { name: 'bb', age: 18 }, { name: 'cc', age: 21 },]//CodeArray.prototype.my_forEach = function (callback) { for (let i = 0; i < this. length; i++) { callback(this[i], i, this) }}//verifyarr.my_forEach((item, index, arr) => { //111 111 if (item. age === 18) { item.age = 17 return } console.log('111');})

2. map()

map()也用于數組遍歷,與forEach不同的是,它會返回一個新數組,這個新數組由回調函數map返回值接收。COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

const arr = [ { name: 'zt', age: 18 }, { name: 'aa', age: 19 }, { name: 'bb', age: 18 }, { name: 'cc', age: 21 },]Array.prototype.my_map=function(callback){ const res=[] for(let i=0;i{ if(item.age>18){ return item }})console. log(newarr);//[ // undefined, // { name: 'aa', age: 19 }, // undefined, // { name: 'cc', age: 21 }//]

3. filter()

filter()用于過濾滿足條件的元素,并返回一個新數組。COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

const arr = [ { name: 'zt', age: 18 }, { name: 'aa', age: 19 }, { name: 'bb', age: 18 }, { name: 'cc', age: 21 },]Array.prototype.my_filter = function (callback) { const res = [] for (let i = 0; i < this. length; i++) { callback(this[i], i, this) && res. push(this[i]) } return res}//verifylet newarr = arr.my_filter((item, index, arr) => { return item. age > 18})console.log(newarr); [ { name: 'aa', age: 19 }, { name: 'cc', age: 21 } ]

4. reduce()

reduce()用于將數組中的所有元素按照指定的規則進行合并計算,并返回一個最終值。COF28資訊網——每日最新資訊28at.com

reduce() 接收兩個參數:回調函數、初始值(可選)。COF28資訊網——每日最新資訊28at.com

回調函數中接收有四個參數:初始值或者存放最后一個回調函數的返回值、每一項的值、下標、數組本身。COF28資訊網——每日最新資訊28at.com

如果沒有提供初始值,則從第二項開始,將第一個值作為第一次執行的返回值。COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

const arr = [ { name: 'zt', age: 18 }, { name: 'aa', age: 19 }, { name: 'bb', age: 18 }, { name: 'cc', age: 21 },]Array.prototype.my_reduce = function (callback,...arg) { let pre,start=0 if(arg.length){ pre=arg[0] } else{ pre=this[0] start=1 } for (let i = start; i < this.length; i++) { pre=callback(pre,this[i], i, this) } return pre}//verifyconst sum = arr.my_reduce((pre, current, index, arr) => { return pre+=current.age},0) console.log(sum); //76

5. fill()

fill() 用于填充數組的所有元素,它會影響原始數組,返回原始數組的修改值。COF28資訊網——每日最新資訊28at.com

fill() 接收三個參數:填充值、起始位置(默認為 0)、結束位置(默認為 this.length-1)。COF28資訊網——每日最新資訊28at.com

左閉右開灌裝原理COF28資訊網——每日最新資訊28at.com

當使用起始位置和結束位置時,它們默認填充整個數組。COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

Array.prototype.my_fill = function (value,start,end) { if(!start&&start!==0){ start=0 } end=end||this.length for(let i=start;i ]6. includes()

想法:COF28資訊網——每日最新資訊28at.com

include()用于判斷數組中是否存在元素,返回值true或falseCOF28資訊網——每日最新資訊28at.com

include() 提供第二個參數支持指定位置開始查找COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

const arr = ['a', 'b', 'c', 'd', 'e']Array.prototype.my_includes = function (item,start) { if(start<0){start+=this.length} for (let i = start; i < this. length; i++) { if(this[i]===item){ return true } } return false}//verifyconst flag = arr.my_includes('c',3) //The element to be searched, from which subscript to start searchingconsole.log(flag); //false

6. join()

join() 用于將數組中的所有元素連接成指定符號的一個字符串COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

const arr = ['a', 'b', 'c']Array.prototype.my_join = function (s = ',') { let str = '' for (let i = 0; i < this. length; i++) { str += `${this[i]}${s}` } return str. slice(0, str. length - 1)}//verifyconst str = arr. my_join(' ')console.log(str); //a b c

7. find()

find()用于返回數組中第一個滿足條件的元素,找不到元素返回undefined。COF28資訊網——每日最新資訊28at.com

find()的參數是一個回調函數。COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

const arr = [ { name: 'zt', age: 18 }, { name: 'aa', age: 19 }, { name: 'bb', age: 18 }, { name: 'cc', age: 21 },]Array.prototype.my_find = function (callback) { for (let i = 0; i < this.length; i++) { if(callback(this[i], i, this)){ return this[i] } } return undefined}//verifylet j = arr.my_find((item, index, arr) => { return item.age > 19 })console.log(j); //{ name: 'cc', age: 21 }

8. findIndex()

findIndex()用于返回數組中第一個滿足條件的,索引找不到返回-1COF28資訊網——每日最新資訊28at.com

findIndex()的參數是一個回調函數。COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

const arr = [ { name: 'zt', age: 18 }, { name: 'aa', age: 19 }, { name: 'bb', age: 18 }, { name: 'cc', age: 21 },]Array.prototype.my_findIndex = function (callback) { for (let i = 0; i < this.length; i++) { if(callback(this[i], i, this)){ return i } } return -1}let j = arr.my_findIndex((item, index, arr) => { return item.age > 19})console.log(j); //3

9. some()

元素 some() 用于檢查數組中指定的條件是否滿足。COF28資訊網——每日最新資訊28at.com

如果有一個元素滿足條件,則返回 true,并且不會再檢查后面的元素。COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

const arr = [ { name: 'zt', age: 18 }, { name: 'aa', age: 19 }, { name: 'bb', age: 18 }, { name: 'cc', age: 21 },]Array.prototype.my_some = function (callback) { for (let i = 0; i < this. length; i++) { if(callback(this[i], i, this)){ return true } } return false}//verifyconst flag = arr.some((item, index, arr) => { return item. age > 20})console.log(flag); //true

10. every()

every() 用于檢查所有元素是否都滿足指定條件。COF28資訊網——每日最新資訊28at.com

如果有一個條件不滿足,則返回 false,后面的元素不會再執行。COF28資訊網——每日最新資訊28at.com

代碼:COF28資訊網——每日最新資訊28at.com

const arr = [ { name: 'zt', age: 18 }, { name: 'aa', age: 19 }, { name: 'bb', age: 18 }, { name: 'cc', age: 21 },]Array.prototype.my_every = function (callback) { for (let i = 0; i < this.length; i++) { if(!callback(this[i], i, this)){ return false } } return true}//verifyconst flag = arr.my_every((item, index, arr) => { return item.age > 16})console.log(flag); //true

寫在最后

以上就是我今天想與您分享的10個手動編寫的JS數組API的知識內容,如果對您有幫助的話,請記得點贊我,關注我,并將這個知識分享給您的朋友,也許能夠幫助到他。COF28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-76-0.html十個可以手動編寫的 JavaScript 數組 API

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: K6:面向開發人員的現代負載測試工具

下一篇: 線程通訊的三種方法!通俗易懂

標簽:
  • 熱門焦點
  • 6月iOS設備好評榜:第一蟬聯榜首近一年

    作為安兔兔各種榜單里變化最小的那個,2023年6月的iOS好評榜和上個月相比沒有任何排名上的變化,僅僅是部分設備好評率的下降,長年累月的用戶評價和逐漸退出市場的老款機器讓這
  • 5月iOS設備好評榜:iPhone 14僅排第43?

    來到新的一月,安兔兔的各個榜單又重新匯總了數據,像安卓陣營的榜單都有著比較大的變動,不過iOS由于設備的更新換代并沒有那么快,所以相對來說變化并不大,特別是iOS好評榜,老款設
  • 太卷!Redmi MAX 100英寸電視便宜了:12999元買Redmi史上最大屏

    8月5日消息,從小米商城了解到,Redmi MAX 100英寸巨屏電視日前迎來官方優惠,到手價12999元,比發布價便宜了7000元,在大屏電視市場開卷。據了解,Redmi MAX 100
  • Golang 中的 io 包詳解:組合接口

    io.ReadWriter// ReadWriter is the interface that groups the basic Read and Write methods.type ReadWriter interface { Reader Writer}是對Reader和Writer接口的組合,
  • 使用LLM插件從命令行訪問Llama 2

    最近的一個大新聞是Meta AI推出了新的開源授權的大型語言模型Llama 2。這是一項非常重要的進展:Llama 2可免費用于研究和商業用途。(幾小時前,swyy發現它已從LLaMA 2更名為Lla
  • 2天漲粉255萬,又一賽道在抖音爆火

    來源:運營研究社作者 | 張知白編輯 | 楊佩汶設計 | 晏談夢潔這個暑期,旅游賽道徹底火了:有的「地方」火了&mdash;&mdash;貴州村超旅游收入 1 個月超過 12 億;有的「博主」火了&m
  • 消費結構調整丨巨頭低價博弈,拼多多還卷得動嗎?

    來源:征探財經作者:陳香羽隨著流量紅利的退潮,電商的存量博弈越來越明顯。曾經主攻中高端與品質的淘寶天貓、京東重拾&ldquo;低價&rdquo;口號。而過去與他們錯位競爭的拼多多,靠
  • 阿里瓴羊One推出背后,零售企業迎數字化新解

    作者:劉曠近年來隨著數字經濟的高速發展,各式各樣的SaaS應用服務更是層出不窮,但本質上SaaS大多局限于單一業務流層面,對用戶核心關切的增長問題等則沒有提供更好的解法。在Saa
  • 親歷馬斯克血洗Twitter,硅谷的苦日子在后頭

    文/劉哲銘  編輯/李薇  馬斯克再次揮下裁員大刀。  美國時間11月14日,Twitter約4400名外包員工遭解雇,此次被解雇的員工的主要工作為內容審核等。此前,T
Top