zoukankan      html  css  js  c++  java
  • 后盾人: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 ){})  //返回新数组。循环数组,为真时候,保留元素

  • 相关阅读:
    2. Add Two Numbers
    1. Two Sum
    22. Generate Parentheses (backTracking)
    21. Merge Two Sorted Lists
    20. Valid Parentheses (Stack)
    19. Remove Nth Node From End of List
    18. 4Sum (通用算法 nSum)
    17. Letter Combinations of a Phone Number (backtracking)
    LeetCode SQL: Combine Two Tables
    LeetCode SQL:Employees Earning More Than Their Managers
  • 原文地址:https://www.cnblogs.com/jidanbufan/p/14294017.html
Copyright © 2011-2022 走看看