数组专题知识点汇总

数组的创建方式

  1. 利用new关键词创建数组
  2. 利用数组字面量创建数组

访问数组

  1. 索引访问

数组扩展

  1. 空位数组
    • 空元素可以读写,length 属性不排斥空位
    • 空位元素读取返回值是 undefined,但是和元素的值为 undefined 是不同概念
    • forEach() 方法、for/in 语句以及 Object.keys() 方法 在循环空元素的时候回直接跳过
  2. for循环
  3. 解构赋值
  4. 使用slice方法
  5. es6的Array.from()方法
 var arr1 = new Array(2,5,8)
        
        for (let index = 0; index < arr1.length; index++) {
            const element = arr1[index];
            console.log(element);
        }

        function test1() {
            console.log(arguments);
            let arr2 = [...arguments]
            console.log(arr2)
          }        
         test1("1","3","7") 

        function test2(){
            console.log(arguments)
            let arr3 = Array.prototype.slice.call(arguments)
            console.log(arr3)
        }
        test2("2","4","6")

        function test3(){
            console.log(arguments)
            let arr4 = Array.from(arguments)
            console.log(arr4)
        }
        test3("3","5","9")

判断数据是否为数组类型

  1. Array.isArray()

  2. Object.prototype.toString()来判断(需要借助call/apply/bind)

  3. 使用 instanceof 来判断

  4. 使用 constructor 来判断

  5. 不能使用typeof判断,因为数组本身属于对象

    		var a = []
            var b = {}
            var c = 'abc'
            
            //Array.isArray()
            console.log(Array.isArray(a));//true
            console.log(Array.isArray(b));//false
            console.log(Array.isArray(c));//false
    
            //Object.prototype.toString()
            console.log(Object.prototype.toString.call(a))// 数组 [object Array]
            console.log(Object.prototype.toString.call(b))// [object Object]
    
            // instanceof()
            console.log(a instanceof Array) // true
            console.log(b instanceof  Array )// false  
            console.log(c instanceof Array ) // false  
            
            // 因为数组本身属于对象,所以不能用typeof方法
            console.log(typeof(a)); // Object
            console.log( a instanceof Object)// true 
    

    数组相关方法

    1. push

      • 功能: 在原数组末尾添加新元素
      • 参数: 非必须
      • 返回值: 数组长度
      • 是否改变原数组:
    2. unshift

      • 功能: 在原数组开始位置添加元素
      • 参数: 非必须
      • 返回值: 数组长度
      • 是否改变原数组:
    3. pop

      • 功能: 删除数组最后一位元素
      • 参数:
      • 返回值: 删除的元素
      • 是否改变原数组:
    4. shift

      • 功能: 删除数组第一位元素
      • 参数:
      • 返回值: 删除的元素
      • 是否改变原数组:
    5. indexOf

      • 功能: 返回某个指定的字符串值在字符串中首次出现的位置
      • XXX.indexOf(searchvalue,start)
      • 参数: searchvalue->必需:规定需检索的字符串值
      • 参数: start->非必需:规定检索的起始位置,不传则从首位开始
      • 返回值: 字符串中首次出现的下标,没有匹配则返回 -1
      • 是否改变原数组:
    6. reverse

      • 功能: 数组元素对调
      • 参数:
      • 返回值: 颠倒顺序后的数组
      • 是否改变原数组:
    7. concat

      • 功能: 数组合并任意值
      • 参数: 任意
      • 返回值: 合并后的数组
      • 是否改变原数组:
    8. slice

      • 释义:把…切成(薄)片

      • 功能: 对数组进行截取

      • 参数: slice(start,end)

      • 返回值: 截取后的数组(不包括end的元素)

      • 是否改变原数组:

    9. splice

      • 释义:连接;拼接
      • 功能: 该方法向或者从数组中添加或者删除项目
      • 参数: splice(index, Deletehowmany, newitemadded)
      • 返回值: 返回被删除的项目
      • 是否改变原数组:
    10. slice 与 splice 区别

      • 两者都有截取功能,且都返回被截取的结果并生成一个数组
      • splice,还有替换功能,即在截取的地方,添加需要添加的内容
      • slice不会改变原数组,splice会改变原数组
      		var  arr = [2,3,4]
              console.log(arr.push(5)) // 4
              console.log(arr) //[2, 3, 4, 5]
      
              console.log(arr.unshift(1))// 5
              console.log(arr) //[1,2, 3, 4, 5]
      
              console.log(arr.shift()) // 1
              console.log(arr) // [2, 3, 4, 5]
      
              console.log(arr.pop()) // 5
              console.log(arr) // [2,3,4]
      
              var word = "abcdefgh"
      
              console.log(word.indexOf('c')) // 2
              console.log(word.indexOf('d')) // 3
              console.log(word.indexOf('d',1)) // 3
              console.log(word.indexOf('d',2)) // 3
              console.log(word.indexOf('d',3)) // 3
              console.log(word.indexOf('d',4)) // -1
      
              console.log(arr.reverse()) // [4, 3, 2]
              console.log(arr) // [4, 3, 2]
      
              console.log(arr.concat(word)) //[4, 3, 2, "abcdefgh"]
              
               var newArr = [1,2,3,4,5,6,7,8,9]
               console.log(newArr.slice(2,4)); //[3,4]
               console.log(newArr)// [1, 2, 3, 4, 5, 6, 7, 8, 9]
      
              //  出现负数,负数+length,再划分
              console.log(newArr.slice(2,-3))//[3,4,5,6]
              // 出现负数,负数的绝对值大于length,则截取整个
              console.log(newArr.slice(-20))// [1, 2, 3, 4, 5, 6, 7, 8, 9] 
      
              console.log(newArr.splice(4,1,'five'))//[5]
              console.log(newArr) //[1, 2, 3, 4, "five", 6, 7, 8, 9]
              console.log(newArr.splice(4,1)) //  ["five"]
              console.log(newArr) //  [1, 2, 3, 4, 6, 7, 8, 9]
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值