zoukankan      html  css  js  c++  java
  • JavaScript数组和数学对象

    数组的创建:

           var ary1 = new Array();
           console.log(ary1);//[]

    var ary2= new Array(2,3,4,5); console.log(ary2); //[2,3,4,5] var ary3= new Array(4); //传一个参数,为数字,代表数组长度 console.log(ary3); //[empty*4] ary3.length = 8; // 数组的length属性可读可写 console.log(ary3); //[empty*8]

    数组的方法:

    push()

    • 功能:在数组的末尾添加项;
    • 参数:添加进数组的项,一项或者多项;
    • 返回值:数字的新长度;
    • 原数组发生改变

    pop()

    • 功能:在数组的末尾删除一项;
    • 参数:不需要;
    • 返回值:删除的项;
    • 原数组发生改变

    unshift()

    • 功能:在数组的开头添加项;
    • 参数:添加进数组的项,一项或者多项;
    • 返回值:数字的新长度;
    • 原数组发生改变

    shift()

    • 功能:在数组的开头删除一项;
    • 参数:不需要;
    • 返回值:删除的项;
    • 原数组发生改变

    splice()

    • 功能:截取数组中的指定项;
    • 参数:
              (n) 从索引n开始截取到数组末尾
              (n,m) 从索引n开始截取m项
              (n,m,x,y,z) m之后的参数作为数组项从截取的位置添加进数组
    • 返回值:截取项组成的新数组;
    • 原数组发生改变

    slice()

    • 功能:从数组中复制项;
    • 参数:
      (n) 从索引n开始复制到数组末尾
      (n,m) 从索引n开始复制到索引m(不包括m)
    • 返回值:复制出来的项组成的数组;
    • 原数组不发生改变

    join()

    • 功能:把数组以指定的连接符,连成字符串;
    • 参数:连接符,默认是逗号;
    • 返回值:字符串;
    • 原数组不发生改变
        var str1 = ary.join('<span>'+val+'</span>');
        p.innerHTML = str1;

    concat()

    • 功能:把数组或数组项拼接成一个数组;
    • 参数:单项或者数组;
    • 返回值:拼接好的数组;
    • 原数组不发生改变

    indexOf()

    • 功能:查找一个值在数组中的索引;
    • 参数:要查找的项;
    • 返回值:索引;
    • 原数组不发生改变

    reveres()

    • 功能:倒序数组;
    • 参数:不需要;
    • 返回值:倒序后的原数组;
    • 原数组发生改变

    sort()

    • 功能:对数组排序;
    • 参数:不传参数,默认按照字符的编码升序排列;
                  排数字:
                  function(a,b){ return a-b};升序
                  function(a,b){ return b-a};降序
                  排英文字母:
                  function(a,b){ return a.localeCompare(b)};升序
                  function(a,b){ return b.localeCompare(a)};降序
                  排中文:
                  function(a,b){ return a.localeCompare(b,'zh')};升序
                  function(a,b){ return b.localeCompare(a,'zh')};降序
                  根据数组中每一项的一个属性排序
                  function(a,b){ return a.name.localeCompare(b.name,'zh')};
                  function(a,b){ return a.age-b.age};
    • 返回值:排序后的原数组;
    • 原数组发生改变

    数组的迭代方法:

            类数组不能直接使用数组提供的方法,需要把类数组转成数组才能使用

     ```
                var  ary = [3,7,4,9];
                var res = ary.every(function(item,index){
                    return item > 2;
                });
                console.log(res);  //true

    var res = ary.some(function(item,index){ return item > 5 ; }) console.log(res); //true

    var res = ary.filter(function(item,index){ return item%2==1 ; }) console.log(res); // [3,7,9]
    // forEach就是单纯把数组遍历一下 var res = ary.forEach(function(item,index){ console.log(index + ':' +item); }) console.log(res); //undefined var res = ary.map(function(item,index){ return item+1; }) console.log(res); //[4, 8, 5, 10] ```

    数学对象:

    • Math.abs()——取绝对值;
    • Math.ceil()——向上取整;
    • Math.floor()——向下取整;
    • Math.min()——取最小值;
    • Math.max()——取最大值;
    • Math.pow(n,m)——n的m次方;
    • Math.sqrt(m)——对m开平方;
    • Math.random()——获取0-1随机浮点数;
    • 获取n-m之间的随机数
                  function getRandom(n,m){
                      return parseInt(Math.random()*(m-n+1)+n);
                  }
  • 相关阅读:
    开发day7
    开发day6
    开发day5
    开发day4
    开发day3
    开发day2
    开发day1
    假期学习2/8
    什么是栈帧
    JDK、JRE和JVM到底是什么
  • 原文地址:https://www.cnblogs.com/musong-out/p/11421151.html
Copyright © 2011-2022 走看看