zoukankan      html  css  js  c++  java
  • ES6新增函数总结和range函数实现

    Array.from  类数组,Set,字符串转为数组

    Array.of   不定参数转为数组

    Array.prototype.fill(value,[start],[end]) 对数组在指定范围填充值

    Array.prototype.find() 查找满足条件的首个元素,否则返回undefined

    Array.prototype.findIndex() 查找满足条件的首个元素的索引Array.prototype.includes() 判断数组是否包含指定的值,包含返回true,否则返回false,对象不能检测

    Array.prototype.entries() 用数组的索引和值组成的对象生成一个迭代器

    Array.prototype.keys() 用数组的索引生成一个迭代器

    Array.prototype.values() 用数组的值生成一个迭代器

    String.prototype.trim()   去除两端空格

    String.prototype.repeat()  重复字符串生成新字符串

    Sting.prototype.startsWith(str,position)   以指定子字符串开始,开始位置可选

    String.prototype.endsWith(str,position)以指定子字符串结尾,结束位置可选

    String.prototype.includes(str,positon)   是否包含指定的子串,位置可选

    Object.assign  对象合并

    1. range函数实现

    1.1 keys方法

    function range(start,end){
        return [...Array(end - start).keys()].map(item => start + item);
    }

    在简单情形下,可以直接

    [...Array(5).keys()] 来生成一个0到4的序列

    1.2 fill方法

    function range(start,end){
        return Array(end-start).fill(0).map((el,i) => start + i);
    }

     1.3 Array.from方法

    function range(end) {
        return Array.from({ length: end }, (_, index) => index);
    }

    2. 数组去重

    function unique(array) {
      return Array.from(new Set(array));
    }
    function unique(array) { return [...new Set(array)]; }

  • 相关阅读:
    用R作Polar图等
    R语言绘制空间热力图
    Spark 基础及RDD基本操作
    Bars, rectangles with bases on x-axis
    spark dataframe操作集锦(提取前几行,合并,入库等)
    【R】用 ggplot2 绘制漂亮的分级统计地图
    Rattle:数据挖掘的界面化操作
    R语言进阶之4:数据整形(reshape)
    ggplot2——简介
    python复习冒泡排序
  • 原文地址:https://www.cnblogs.com/mengff/p/11195202.html
Copyright © 2011-2022 走看看