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