zoukankan      html  css  js  c++  java
  • 007-js中的Array常用方法

    原文:http://www.runoob.com/jsref/jsref-obj-array.html

    摘要常用方法

      const array=new Array();
      // 1 增加元素
      // 1.1 向数组的末尾添加一个或更多元素,并返回新的长度。
      for (var i = 0; i < 5; i++) {    
        array.push({id:i+1,value:'value-'+i})
      }
      console.log('1.1',array)
      // 1.2 向数组的开头添加一个或更多元素,并返回新的长度。
      array.unshift({id:0,value:'value-'+0})
      console.log('1.2',array)
    
      // 2 删除
      // 2.1 删除并返回数组的第一个元素
      array.shift();
      console.log('2.1',array)
      // 2.1 删除数组的最后一个元素并返回删除的元素。
      array.pop();
      console.log('2.1',array)
    
      // 3 遍历
      // 3.1 for 原始方式 //可以中断 使用 break,continue,return
      for (var i = 0; i < array.length; i++) {
        let a=array[i]
      }
      // 3.2 forEach 数组每个元素都执行一次回调函数。返回值 undefined。 不可以 通过break,return 跳出循环
      var s=array.forEach(function (currentValue,index) {
        // body...
        console.log(currentValue)
      })
      // 3.3 通过指定函数处理数组的每个元素需要return新元素,并返回处理后的数组。
      var s2=array.map(function (currentValue,index) {
        // body...
        return currentValue.id+1
      })
      console.log(s2)
    
      //4.条件获取
      //4.1 filter 检测数值元素,并返回符合条件所有元素的数组。没有符合的 空数组 []
      var s3=array.filter(function (currentValue,index) {
        // body...
        //返回 bool
        return currentValue.id>10
      })
      console.log(s3)
      //4.2 find 返回符合传入测试(函数)条件的数组元素。没有符合的返回 undefined
      var s4=array.find(function (currentValue,index) {
        // body...
        //返回 bool
        return currentValue.id>10
      })
      console.log(s4)
  • 相关阅读:
    hdu 4474 大整数取模+bfs
    Codeforces Mafia
    hdu 4750 Count The Pairs(并查集)
    zoj 3659 Conquer a New Region(并查集)
    zoj 3656
    poj 3678 Katu Puzzle(Two Sat)
    UVa 11235 RMQ
    hdu 4768 Flyer (二分)
    hdu 4762 Cut the Cake概率公式
    Ural 1046 Geometrical Dreams(解方程+计算几何)
  • 原文地址:https://www.cnblogs.com/bjlhx/p/10494738.html
Copyright © 2011-2022 走看看