• 后盾人:JS第四章-数组


    数组属于引用类型

    数组的方法:

    Array.of()  //创建数组

    array.isArray([])  //true   判断是否为数组,返回布尔值

    [1,2].toString()  //转换为字符串

    String([1,2])  //转换字符串

    [1,2,3].join('-')  //‘’1-2-3‘’ 返回字符串

    Arrat.form(div,fun(){})  //只有有length属性就能转换数组,对象默认不行。还可以对每个对象添加方法

    push(1,2)  //数组后面增加元素,返回新数组length

    unshift()  //在前面增加,返回添加后新数组的length

    shift()  //在前面移除元素,返回删除的值

    pop()  //删除数组最后一位,返回删除元素

    fill('后盾人',1,3)  //在数组1-3之间替换成“’后盾人”

    slice()  //截取元素,不改变原数组

    splice(2,1,“后盾人”)  //数组下标,删除个数,增加元素(改变原数组

    //splice()小实例  移动数组元素位置,不改变原来数组
    function move(array, from, to){
        if(from < 0 || to >= array.length){
            console.error('参数错误')
            return;
        }
        const newArray = [...array] //不改变原来数组
        let item = newArray.splice(from, 1)
        newArray.splice(to, 0, ...item)
        return newArray;
    }
        

     清空数组

    array = [1,2]  array = hd

    array = []  //不会改变内存地址.hd = [1,2]

    array.length = 0 //清空数组,修改原数组。hd = []


    split('-')  //把字符串以(-)拆分为数组

    copyWithin(2,1,2)  //复制元素。(起始复制位置-2,复制内容的起始位置-1,复制内容的结束位置-2)


    循环数组

    find()  //循环数组,返回true退出循环,返回当前值

    findIndex()  //循环数组,返回true退出循环,返回当前索引值

    sort(fc(a,b){return a-b})   // 排序数组,a-b  从小打到; b-a 从大到小

    forRach(function(item, index, array))  //循环数组,支持dome元素

     every(function(value, index, arry){})  //循环数组,返回布尔值。返回false即退出循环

     some(function(value, index, arry){})  //循环数组,返回布尔值。返回true即退出循环

    //推荐多使用

    reduce(function(pre, value, index, array){},0)  //循环数组。 pre为返回值,后面设置0,第一次为0.不设置为第一个元素

    过滤数组

    filter(function(value, index, arry ){})  //返回新数组。循环数组,为真时候,保留元素

  • 相关阅读:
    java中有趣的unicode转义序列
    react父组件传入子组件的props不更新问题
    create-react-app修改端口号
    转移博客启动中。。。
    idea java界面设置中文
    记录一次 electronjs 12.0.0 安装运行出现cli.js出错、以及获取不到nodeapi的问题(解决办法:版本不对导致的)
    springboot 统一json返回格式,并设置http响应码
    springboot 统一json返回结构
    【Python】【PyPI】twine模块打包python项目上传pypi
    【GitHub】README.md自述文件配置
  • 原文地址:https://www.cnblogs.com/jidanbufan/p/14294017.html
Copyright © 2020-2023  润新知