JS数组

作者: olivia1111 | 来源:发表于2016-01-24 10:38 被阅读23次

    创建数组

    • var array=new Array();
      var array=[];
      var array=[1,6,3];使用逗号分开
      var array=[163,"netease",{color:"red"},[],true];//数组可以包含个类型内容

      var students=[
        {di:1;score:80},
        {di:2;score:84},
        {di:3;score:81}
      ]
      

    数组长度

    • arr.length
      students.length;//3
      students=[];
      students.length;//0

    获取数组元素

    • array[sub]
      students[0];//{di:1;score:80}
      student[0].score;//80,拿到对象中的属性值

    修改数组元素

    • array[].
      student[1].score=60;

    查找数组元素

    • arr.indexOf(searchElement[,fromIndex=0])找到对应的索引位置
      var tel=[110,120,114];
      tel.indexOf(120);//1
      tel.indexOf(119);//-1,没找到为-1

    • arr.forEach(callback[,thisArg])
      遍历所有元素,并执行callback方法
      var editScore=function(item,index,array){item.score+=5};//定义callback方法
      students.forEach(editScore);

    元素空时跳过取下个元素
    var obj = {}, count = 0;
    function logArray(value, index, array) {
    count++;
    obj[count] = value;
    }
    [1, 2, , 4].forEach(logArray);

    重新排序

    • 倒序排列素组,查找数组元素,改变原数组
      arr.reverse()
      var students=[
      {di:1;score:80},
      {di:2;score:84},
      {di:3;score:81}
      ]
      students.reverse();
      students[0].score;//70

    • 按某一元素排序
      arr.sort([compareFunction])
      会改变原有的数组元素顺序
      var byScore=function(a,b){
      return b.score-a.score;
      }//方法:返回值(b分数-a分数)<0,则排序ab,>0排序ba,=0排序不变,此为由大到小排列
      students.sort(byScore);

      var studentNames =["wq","gk","pd"];
      studentNames.sort();按照字符编码排序
      studentNames;//["gk","pd","wq"]
      

    增加元素

    • arr.push(element1,...,elementN)在最后添加
      students.push({id:4,score:90});
      students.push({id:4,score:90},{id:5,score:90});

    • arr.unshift(element1,...,elementN)在开头添加
      students.unshift({id:4,score:90});
      students.unshift({id:4,score:90},{id:5,score:90});
      //{id:4,score:90}
      {di:1;score:80},
      {di:2;score:84},
      {di:3;score:81}

    取出元素

    • arr.shift取出第一个元素
      students.shift();//{di:1;score:80}
      // {di:2;score:84},
      {di:3;score:81}

    • arr.pop取出最后一个元素
      students.pop();// {di:3;score:81}


    • 任意位置删除和添加元素arr.splice(index,howMany[,ele1[,...[,eleN]]])
      howMany:删掉的元素数量
      students.splice(1,1{id:4,score:90});替换一个元素
      students.splice(1,1);删掉一个元素
      students.splice(1,0{id:4,score:90});不删元素,在索引1元素前增加一个元素

    方法
    reverse/sort
    push/unshift 全部改变原数组
    shift/pop
    splice

    复制拷贝元素

    • arr.slice(begin[,end])
      students1 = students.slice(0,2);//不包含结束位置
      students1 = students.slice(0);//拷贝全部数组元素

    合并数组

    • arr.concat(value1,...,valueN)
      value 可以是number等其他格式的
      var NewStudents = students1.concat(students2,students3)

    数组转为字符串

    • arr.join([separator])
      var emails = ["aaa","hhh","yyy"];
      emails.join(";");//"aaa;hhh;yyy"
      emails.join();//"aaa,hhh,yyy"不填内容使用逗号分隔
      emails.join("");//"aaahhhyyy"

    遍历处理元素并产生新的数组

    • arr.map(callback,[,thisArg])
      var scores=[60,70,80,90]
      var addscores=function(item,index,array){
      return item+5;
      }
      scores.map(addscores);//[65,75,85,95]

    数组元素相加之和

    • arr.reduce(callback,[initialValue])
      var sum= function(previousResult,item,index,array){
      return previousResult+item.score
      }
      students.reduce(sum,0);//200,0为返回值的初始值

    相关文章

      网友评论

          本文标题:JS数组

          本文链接:https://www.haomeiwen.com/subject/qrqkkttx.html