看了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对象,以为更加方便遍历,并且键名能够为对象。