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

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

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

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

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

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

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

1.forEach()

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

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

代碼:7Vt28資訊網——每日最新資訊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返回值接收。7Vt28資訊網——每日最新資訊28at.com

代碼:7Vt28資訊網——每日最新資訊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()用于過濾滿足條件的元素,并返回一個新數組。7Vt28資訊網——每日最新資訊28at.com

代碼:7Vt28資訊網——每日最新資訊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()用于將數組中的所有元素按照指定的規則進行合并計算,并返回一個最終值。7Vt28資訊網——每日最新資訊28at.com

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

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

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

代碼:7Vt28資訊網——每日最新資訊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() 用于填充數組的所有元素,它會影響原始數組,返回原始數組的修改值。7Vt28資訊網——每日最新資訊28at.com

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

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

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

代碼:7Vt28資訊網——每日最新資訊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()

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

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

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

代碼:7Vt28資訊網——每日最新資訊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() 用于將數組中的所有元素連接成指定符號的一個字符串7Vt28資訊網——每日最新資訊28at.com

代碼:7Vt28資訊網——每日最新資訊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。7Vt28資訊網——每日最新資訊28at.com

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

代碼:7Vt28資訊網——每日最新資訊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()用于返回數組中第一個滿足條件的,索引找不到返回-17Vt28資訊網——每日最新資訊28at.com

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

代碼:7Vt28資訊網——每日最新資訊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() 用于檢查數組中指定的條件是否滿足。7Vt28資訊網——每日最新資訊28at.com

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

代碼:7Vt28資訊網——每日最新資訊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() 用于檢查所有元素是否都滿足指定條件。7Vt28資訊網——每日最新資訊28at.com

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

代碼:7Vt28資訊網——每日最新資訊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的知識內容,如果對您有幫助的話,請記得點贊我,關注我,并將這個知識分享給您的朋友,也許能夠幫助到他。7Vt28資訊網——每日最新資訊28at.com

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

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

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

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

標簽:
  • 熱門焦點
Top