zoukankan      html  css  js  c++  java
  • 浅学 Array [数组] 的一些方法(以及跳出循环)

    数组 Array [] 的方法:
    有下标,有length


    Array.from(类数组) 类数组转数组
    1.将一个类数组对象或者可遍历对象转换成一个真正的数组。
          (类数组:是一个有length,属性名是数字的对象(像数组,却不能用数组的方法)。比如:获取的一组元素/arguments)
    2.Array.from(类数组,()=>{})还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组。
    3.可以将字符串转换为数组
    4.Array.from参数是一个真正的数组,会返回一个一模一样的新数组。

    >>更多类数组 转 数组的方法,看另一篇随笔<<
    1     let arr = [12,45,97,9797,564,134,45642];
    2     let set = new Set(arr);
    3     console.log(Array.from(set, item => item + 1));// [ 13, 46, 98, 9798, 565, 135, 45643 ]
        
       /*查询类 indexof lastIndexOf includes Array.isArray()

    indexOf 找到数组中指定字符首次出现的索引,需要传参,不会改变原数组,返回位置的索引,找不到返回-1
    lastIndexOf 找到数组中指定字符末次出现的索引,找不到返回-1
    includes 找到数组中是否包含某一项,返回值是布尔值true/false
    Array.isArray() 检测判断是否为数组 */
    1     let ary = [0, 1, 2, 3, "zhufengpeixun", 0, 1, 2, 3];
    2     console.log(ary.indexOf(7));//-1
    3     console.log(ary.lastIndexOf(3));// 8
    4     console.log(ary.includes("zhufengpeixun"));//true
    5     console.log(Array.isArray(ary));//true

    /*截取类 slice
    slice 截取数组指定位置的数据,需要传参,不会改变原数组,返回一个新数组。
    ary.slice(n,m)从第n位开始截到第m-1位*/
    1     let ary1 = [0, 1, 2, 3, 4, 5];
    2     console.log(ary1.slice(2, 4));//[2, 3]

    /*操作类 push pop unshift shift splice findIndex() find()
        push 往数组的末尾添加一个或多个数据,需要传参, 会改变原数组,返回值是新数组的length。
        pop() 往数组的末尾删除一个数据, 不需要传参,会改变原数组,返回值就是删除的那个数据。
        unshift 往数组的首位添加一个或多个数据,需要传参, 会改变原数组,返回值是新数组的length。
        shift 往数组的首位删除一个数据, 不需要传参,会改变原数组,返回值就是删除的那个数据。
        splice 能够增、删、改、查数组, 需要传参, 会改变原数组,返回值就是删除的那段数组。
        findIndex 能够返回符合callback条件的那一项的索引,不改变原数组,没有匹配项就返回 -1
        find() 找到数组中符合callback条件的值,而不是索引,不改变原数组,没有匹配项为undefined
         */
    1     let ary2 = [0, 1, 2, 3, 4, 5];
    2     console.log(ary2.push(6, '7'));//8
    3     console.log(ary2);//[0, 1, 2, 3, 4, 5, 6, "7"]
    4     console.log(ary2.pop());//'7'
    5     console.log(ary2.unshift('f'));//8
    6     console.log(ary2.shift());//'f'
    7     let a = ary2.splice(1, 2, 'f', 'd', 'd');//从第一项开始,删除2个,添加"f",'d','d'
    8     console.log(ary2);//[0, "f", "d", "d", 3, 4, 5, 6]
    9     console.log(a);//[1, 2]
     10   let b = ary2.findIndex((e)=>{
          return e === 3
        })
        console.log(b)//复合条件的第一项的索引
     

    /*连接 join concat
    join 用指定字符拼接数组 需要传参,不会改变原数组,返回值为字符串。
    concat 把两个数组拼接成一个,需要传参,不会改变原数组,返回值为拼接后的新数组。
    */
    1     let ary3 = ['zhu', 'uo'];
    2     let ary4 = [520, 1314];
    3     console.log(ary3.join('g'));//'zhuguo'
    4     console.log(ary3);//["zhu", "uo"]
    5     console.log(ary3.concat(ary4));//["zhu", "uo", 520, 1314]

    /*翻转 reverse
    reverse 翻转数组,不需要传参,改变原数组,返回值是翻转后的数组
    */
    1     ary5 = [1, 2, 3, 4, 5];
    2     console.log(ary5.reverse());//[5, 4, 3, 2, 1]
    3     console.log(ary5);

    /*排序 sort
    sort 参数是一个函数,函数有两个形参,return a-b是升序,b-a是降序
    */
    1     ary5 = [4, 2, 5, 3, 1];
    2     ary5.sort(function (a, b) {
    3         return a - b
    4     });
    5     console.log(ary5);//[1, 2, 3, 4, 5]

    //数组包对象
    ary5=[{age:18},{age:19}]
    ary5.sort(function (a, b) {
      return a.age - b.age
    });


    循环 forEach 、map 、filter 、some 、every 、reduce(缩减,最终成为一个数字,完成累加的效果)
    ??????????????????以后用到再补充定义??????????????????????

      forEach 专门循环数组的,方法用于调用数组的每个元素,并将元素传递给回调函数,有多少项就执行多少次。没有返回值
        注意: forEach() 对于空数组是不会执行回调函数的。
        两个参数: 1.回调函数(1.每一个数据(必须)2.索引(可选)3.整个数组(可选))
             2.改变this的方向(可选,如果这个参数为空,this会指向undefined)

      map 循环数组,返回一个新数组,有多少项就执行多少次。(返回判断结果)不会改变原数组的值

      every 数组中有一个false,返回值就是false
          数组中全为true,返回值就是true

      some 在数组中只要有一个成立,返回值就为true

      filter 过滤,返回一个过滤后的数组。(返回符合判断的结果),不会改变原数组

      continue 跳过本次循环,进入下一次循环
      break 跳出(结束)整个循环
      return 终止此作用域 return 后面的所有代码

    (continue 和 break 在循环中的详细对比)
     1     let arr6 = [2, 3, 5, 6, 8, 9];
     2     let b = arr6.forEach(function (ele, i, all) {
     3         //console.log(ele);//2  3  5  6  8  9
     4         //console.log(i);//0  1  2  3  4  5
     5         //console.log(all);//[2, 3, 5, 6, 8, 9]  而且返回了length 次
     6         console.log(this);//#document   输出了length次
     7         return 1
     8     }, document);
     9     console.log(b);//undefined
    10 /*****************************map********************************/
    11     let c = arr6.map(function (ele, i, all) {
    12         //console.log(ele);//2  3  5  6  8  9
    13         //console.log(i);//0  1  2  3  4  5
    14         //console.log(all)//[2, 3, 5, 6, 8, 9]
    15         return ele = 2
    16     });
    17     console.log(c);//[true]
    18 /****************************every********************************/
    19     let d = arr6.every(function (item,index) {
    20 //        console.log(item);//2  3  5  6  8  9
    21 //        console.log(index);//0  1  2  3  4  5
    22         return item>2//条件中一个不成立就为false,全部成立就为true。
    23     });
    24     console.log(d);//true|false
    25 /***************************some***********************************/
    26     let e = arr6.some(function (item,index) {
    27         return item>4//条件中有一个成立就为true,全部不成立就为false。
    28     });
    29     console.log(e);//true|false
    30 /*****************************filter********************************/
    31     let f = arr6.filter(function (item,index) {
    32         return item>5;//把符合条件的放在一个数组总
    33     });
    34     console.log(f);//[6, 8, 9]

     

    缩减:reduce( 回调函数,初始值 )

             当需要计算、统计数组中的每一项的结果时用它

             这一次的结果需要上一次的返回

    通多一些算法,将一个数组缩减成一个。比如:累加、累减、累乘、累除等等。

       arr.reduce((a,b)=>{ return a+b },0)

    let arr = [2, 4, 6, 8, 10];
    let a = arr.reduce((a, b) => {//a:代表上一次的结果。  b:是本次循环的那个。
        return a + b
    },0);//零是初始值
    console.log(a);//累加  结果是 30
    let ary = arr.reduce((a,b)=>{
        a.push(b);
        return a
    },[]);  //初始值可以是数字、数组[]、对象{}
    console.log(ary);//[2, 4, 6, 8, 10]
  • 相关阅读:
    Pytorch安装
    使用Inception-v3进行图像分类
    Tensorflow模型保存与载入
    LSTM用于MNIST手写数字图片分类
    卷积神经网络应用于MNIST数据集分类
    手工设计神经MNIST使分类精度达到98%以上
    关于优化器的选择
    手动设计神经网络进行MNIST分类
    Matplotlib学习
    Apiview使用方法
  • 原文地址:https://www.cnblogs.com/MrZhujl/p/9851805.html
Copyright © 2011-2022 走看看