es6遍历树结构并判断_ES6和ES5中数据结构的遍历
看了ES6就感覺(jué)各種數(shù)據(jù)結(jié)構(gòu)的遍歷方法好多好混亂,就寫(xiě)下來(lái)總結(jié)下,看看應(yīng)用場(chǎng)景上有什么區(qū)別
Array:
ES5:
(1)Array.prototype.forEach(function(item,index,array){...})
(2)Array.prototype.map(function(value,index,array){...//return value,該值會(huì)被插入新數(shù)組})映射為一個(gè)新數(shù)組
(3)Array.prototype.some(function(item){...//條件})數(shù)組中某一項(xiàng)滿足則停止執(zhí)行,并且返回true
(4)Array.prototype.every(function(item){...//條件})數(shù)組中有一項(xiàng)不滿足則停止執(zhí)行,并且返回false.
(5)Array.prototype.filter(function(item){...//return true或者false})返回過(guò)濾后的新數(shù)組
(6)Array.prototype.indexOf(item)
(7)Array.prototype.lastIndexOf(item)
(8)Array.prototype.reduce(function (previous, current, index, array) {...return value//返回值作為下一次循環(huán)的previous的值})
(9)Array.prototype.reduceRight同上,但是index的初始值為array.length-1
ES6:
(1)Array.from(parameter),用的最多應(yīng)該是將set轉(zhuǎn)化為Array,或者將元素選擇器的結(jié)果轉(zhuǎn)化為數(shù)組
(2)Array.of(parameter)消除new Array(parameter)由于參數(shù)個(gè)數(shù)不同而出現(xiàn)的重載
(3)Array.prototype.copyWithin(target, start = 0, end = this.length)沒(méi)想到有什么好用的
(4)Array.prototype.find(function(value, index, arr) {...//條件})找到第一個(gè)返回值為true的成員
(5)Array.prototype.findIndex(function(value.index,arr){...//條件})作用同上,返回index
(6)Array.prototype.keys()獲取鍵名遍歷器
(7)Array.prototype.values()獲取鍵值遍歷器
(8)Array.prototype.entries()獲取鍵值對(duì)遍歷器
Set數(shù)據(jù)結(jié)構(gòu)
該數(shù)據(jù)結(jié)構(gòu)更新或創(chuàng)建時(shí)會(huì)去重,類似===但是在這里NAN和NAN是相等的
(1)Set.prototype.add(parameter)
(2)Set.prototype.delete(parameter)
(3)Set.prototype.has(parameter)
(4)Set.prototype.clear()
(5)Set.prototype.keys()返回鍵名的遍歷器
(6)Set.prototype.values()返回鍵值遍歷器
(7)Set.prototype.entries()返回鍵值對(duì)遍歷器
(8)Set.prototype.forEach(function(value.key,set){})遍歷
Map數(shù)據(jù)結(jié)構(gòu)
鍵值對(duì)的集合,但是鍵名可以為對(duì)象,當(dāng)鍵名為對(duì)象時(shí)判斷他的內(nèi)存地址相同則認(rèn)為鍵名相同
(1)Map.prototype.set(key,value)
(2)Map.prototype.get(key)
(3)Map.prototype.has(key)
(4)Map.prototype.delete(key)
(5)Map.prototype.clear()
(6)Map.prototype.keys()
(7)Map.prototype.values()
(8)Map.prototype.entries()
(9)Map.prototype.forEach(function(value,key,map){...})
這里需要注意map和json的轉(zhuǎn)換,具體可以參考阮一峰的文章
總結(jié),個(gè)人感覺(jué)set和array除了去重沒(méi)什么區(qū)別,而且他們之間可以相互轉(zhuǎn)換,想不出來(lái)有應(yīng)用場(chǎng)景上的區(qū)別。map對(duì)象則相比ES5的時(shí)候的Object對(duì)象,覺(jué)得更加方便遍歷,而且鍵名可以為對(duì)象。
總結(jié)
以上是生活随笔為你收集整理的es6遍历树结构并判断_ES6和ES5中数据结构的遍历的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 擦窗机器人测试标准_擦窗机器人,我选择玻
- 下一篇: 单片机 串口 波特率 c语言 9600