摘要:小明是中簡(jiǎn)單,最直接的一種用法,寫(xiě)起來(lái)較麻煩,循環(huán)要執(zhí)行確定的次數(shù),通常使用循環(huán)。
let arr = [2, 4, 6, 56, 7, 88]; let obj = { name: "小明", age: 18, hobby: "run,song,game" }; let str="xiaoming"for
for是js中簡(jiǎn)單,最直接的一種用法,寫(xiě)起來(lái)較麻煩,循環(huán)要執(zhí)行確定的次數(shù),通常使用 for 循環(huán)。for類(lèi)循環(huán)都可以break,return.
for (let i = 0; i < arr.length; i++) { console.log(i + ":" + arr[i]) //0:2 1:4 2:6 3:56 4:7 5:88 }while
很容易死循環(huán)
let arr=[1,0,8,7],i=0; while(ido while 很容易死循環(huán)
let arr=[1,0,8,7],i=0; do{ console.log(i+":"+arr[i]);//0:1 1:0 2:8 3:7 i++ }while(ifor in 遍歷的key,key為string類(lèi)型,也會(huì)循環(huán)原型鏈中的屬性,適用于對(duì)象
for(let key in obj){ console.log(key+":"+obj[key]) //name:小明 age:18 hobby:run,song,game }for of遍歷的value
for(let value of arr){ console.log(value) //2 4 6 56 7 88 } for (let [key, value] of arr.entries()) { console.log(key+":"+value); //0:2 1:4 2:6 3:56 4:7 5:88 } for (let [key, value] of Object.entries(obj)) { console.log(key+":"+value); //name:小明 age:18 hobby:run,song,game }forEach最節(jié)省內(nèi)存的一種,但是不能break,也不能用return
arr.forEach((value,key)=>{ console.log(key + ":" + value) //0:2 1:4 2:6 3:56 4:7 5:88 }) Object.keys(obj).forEach((value)=>{ console.log(value) //"name", "age", "hobby" })map同forEach寫(xiě)法一樣,循環(huán)每一個(gè)的時(shí)候就相當(dāng)于在內(nèi)存中新建了一個(gè)數(shù)據(jù),比較占內(nèi)存,與forEach不同的是,它可以return。返回?cái)?shù)組。
arr.map((value,index)=>{ console.log(index+":"+value) //0:2 1:4 2:6 3:56 4:7 5:88 }) Object.values(obj).map((value,key)=>{ console.log(key+":"+value) //0:小明 1:18 2:run,song,game })Object.keys()/values()console.log(Object.keys(obj)) // (3) ["name", "age", "hobby"] console.log(Object.values(obj)) // (3) ["小明", 18, "run,song,game"]Array Array.prototype.reduce()累加器
語(yǔ)法:arr .reduce(callback [累加器累加的值,當(dāng)前元素的值,當(dāng)前元素的索引(可選),]) arr.reduce((acc,value)=>acc+value) //2+4+6+56+7+88=163 arr.reduce((acc,value)=>{ return acc+value },5) //5+2+4+6+56+7+88=168,第一個(gè)為此時(shí)設(shè)置的值為index=0開(kāi)始 //合并數(shù)組 [[0, 1], [2, 3], [4, 5]].reduce( ( acc, cur ) => acc.concat(cur), [] //[0, 1, 2, 3, 4, 5] ); //統(tǒng)計(jì)出現(xiàn)次數(shù) var names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"]; names.reduce(function (allNames, name) { if (name in allNames) { allNames[name]++; } else { allNames[name] = 1; } return allNames; }, {}); // { "Alice": 2, "Bob": 1, "Tiff": 1, "Bruce": 1 } //按順序運(yùn)行promise function runPromiseInSequense(arr) { return arr.reduce((promiseChain, currentPromise) => { return promiseChain.then((chainedResult) => { return currentPromise(chainedResult) .then((res) => res) }) }, Promise.resolve()); } // promise function 1 function p1() { return new Promise((resolve, reject) => { resolve(5); }); } // promise function 2 function p2(a) { return new Promise((resolve, reject) => { resolve(a * 2); }); } // promise function 3 function p3(a) { return new Promise((resolve, reject) => { resolve(a * 3); }); } const promiseArr = [p1, p2, p3]; runPromiseInSequense(promiseArr) .then((res) => { console.log(res); //5+5*2+5*3= 30 });Array.prototype.every()遍歷所有元素,得到一個(gè)bool值,要所有條件都相符才會(huì)返回true
arr.every(x =>{ x >= 10}); // falseArray.prototype.some()得到一個(gè)布爾值,只要有一個(gè)符合條件就可返回true
arr.some(item=>{ return item>10 //true })Array.prototype.filter()返回一個(gè)符合條件的新數(shù)組
arr.filter(item=>{ return item>10 //(2) [56, 88] })Array.prototype.find()返回符合條件的數(shù)組的值或數(shù)組的對(duì)象,找不到返回undefined
arr.find(item=>item==2)//2Array.indexof(),findIndex()/includs()查找。返回index/-1,boolean Array.prototype.fill(填充的值,開(kāi)始索引,結(jié)束索引)[1, 2, 3].fill(4, 1, 2); // [1, 4, 3]Array.sort()排序
arr.sort((a,b)=>a-b)//升序 arr.sort((a,b)=>b-a)//降序Object Object.creact(proto)proto:一個(gè)對(duì)象,作為創(chuàng)建新對(duì)象的原型或null
用指定的原型對(duì)象和屬性創(chuàng)建一個(gè)新的對(duì)象//來(lái)實(shí)現(xiàn)類(lèi)式繼承,單繼承 fuction Shape(){ this.x=0; this.y=0; } Shape.prototype.move=function(x,y){ this.x += x; this.y += y } function Rectangle(){ Shape.call(this) } Rectangle.prototype=Object.create(Shape.prototype) var rect=new Rectangle() rect instanceof Rectangle //true rect instanceof Shape //trueObject.defineProperty(目標(biāo)對(duì)象,給對(duì)象要加的屬性);返回加了屬性之后的對(duì)象 Object.getOwnPropertyNames(目標(biāo)對(duì)象);返回帶有該對(duì)象屬性的數(shù)組 Object.is(value1,value2);判斷2個(gè)值是否相同,返回布爾值
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/107116.html
摘要:遍歷器對(duì)象調(diào)用方法后,拋出的異常被函數(shù)體捕獲以后,會(huì)附帶執(zhí)行下一條語(yǔ)句。 iterator迭代器 在ES6之前遍歷數(shù)組的方法有以下四種: // 第一種 for(var i = 0; i < array.length; i++){ console.log(array[i]) } // 第二種 array.forEach(function(item,index){ cons...
在之前的文章中我們有講過(guò)樹(shù)的相關(guān)知識(shí),例如,樹(shù)的概念、深度優(yōu)先遍歷和廣度優(yōu)先遍歷。這篇文章講述了一個(gè)特殊的樹(shù)——二叉樹(shù)。 什么是二叉樹(shù) 二叉樹(shù)是每個(gè)節(jié)點(diǎn)最多只能有兩個(gè)子節(jié)點(diǎn)的樹(shù),如下圖所示: 一個(gè)二叉樹(shù)具有以下幾個(gè)特質(zhì): 要計(jì)算在每層有多少個(gè)點(diǎn),可以依據(jù)公式2^(i-1)個(gè)(i是樹(shù)的第幾層); 如果這顆二叉樹(shù)的深度為k,那二叉樹(shù)最多有2^k-1個(gè)節(jié)點(diǎn); 在一個(gè)非空的二叉樹(shù)中,若使...
摘要:不過(guò)讓流行起來(lái)的原因應(yīng)該是是目前所有主流瀏覽器上唯一支持的腳本語(yǔ)言。經(jīng)過(guò)測(cè)試,數(shù)字字符串布爾日期可以直接賦值,修改不會(huì)產(chǎn)生影響。再考慮對(duì)象類(lèi)型為或者的情況。對(duì)于結(jié)果聲明其類(lèi)型。判斷對(duì)象的類(lèi)型是還是,結(jié)果類(lèi)型更改。 轉(zhuǎn)載自我的個(gè)人博客 歡迎大家批評(píng)指正 1. 第一個(gè)頁(yè)面交互 這里最需要學(xué)習(xí)的老師的代碼中,每一部分功能都由函數(shù)控制,沒(méi)有創(chuàng)建一個(gè)全部變量。且最后有一個(gè)函數(shù)來(lái)控制執(zhí)行代碼...
摘要:關(guān)注于運(yùn)行中的內(nèi)存信息的展示,用可視化的方式還原了,有助于理解內(nèi)存管理。背景運(yùn)行過(guò)程中的大部分?jǐn)?shù)據(jù)都保存在堆中,所以性能分析另一個(gè)比較重要的方面是內(nèi)存,也就是堆的分析。上周發(fā)布了工具,可以用來(lái)動(dòng)態(tài)地展示的結(jié)果,分析各種函數(shù)的調(diào)用關(guān)系。 OneHeap 關(guān)注于運(yùn)行中的 JavaScript 內(nèi)存信息的展示,用可視化的方式還原了 HeapGraph,有助于理解 v8 內(nèi)存管理。 ...
摘要:區(qū)別一是中發(fā)布的。是作為標(biāo)準(zhǔn)的一部分在中發(fā)布的,而它不是標(biāo)準(zhǔn)的一部分。這將意味著存在各種瀏覽器的兼容性問(wèn)題。遍歷對(duì)象,由于沒(méi)辦法提供理想的遍歷,因而只能選擇其他方法。 區(qū)別一: for in是javascript 1.0 中發(fā)布的。 for each in是作為E4X標(biāo)準(zhǔn)的一部分在javascript 1.6中發(fā)布的,而它不是ECMAScript標(biāo)準(zhǔn)的一部分。 這將意味著存在各種瀏覽器...
閱讀 3288·2021-09-30 09:47
閱讀 2306·2021-09-10 10:51
閱讀 1910·2021-09-08 09:36
閱讀 2939·2019-08-30 12:56
閱讀 3045·2019-08-30 11:16
閱讀 2634·2019-08-29 16:40
閱讀 3005·2019-08-29 15:25
閱讀 1642·2019-08-29 11:02