ES6和ES5中數據結構的遍歷

看了ES6就感受各類數據結構的遍歷方法好多好混亂,就寫下來總結下,看看應用場景上有什麼區別json

Array:數組

ES5:數據結構

(1)Array.prototype.forEach(function(item,index,array){...})this

(2)Array.prototype.map(function(value,index,array){...//return value,該值會被插入新數組})映射爲一個新數組prototype

(3)Array.prototype.some(function(item){...//條件})數組中某一項知足則中止執行,而且返回true對象

(4)Array.prototype.every(function(item){...//條件})數組中有一項不知足則中止執行,而且返回false.內存

(5)Array.prototype.filter(function(item){...//return true或者false})返回過濾後的新數組get

(6)Array.prototype.indexOf(item)it

(7)Array.prototype.lastIndexOf(item)io

(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對象,以爲更加方便遍歷,並且鍵名能夠爲對象。

相關文章
相關標籤/搜索