zoukankan      html  css  js  c++  java
  • 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对象,觉得更加方便遍历,而且键名可以为对象。

  • 相关阅读:
    Web前端开发中的各种CSS规范
    SVN简明课程
    使用django-compressor压缩静态文件
    今日头条视频Url嗅探
    python 异常类型
    抓包分析工具备注
    电子签章盖章之jQuery插件jquery.zsign
    程序员读书雷达
    在csdn里markdown感受
    如何在无趣的世界里,做一个有趣的人?
  • 原文地址:https://www.cnblogs.com/wangwei1314/p/5625018.html
Copyright © 2011-2022 走看看