JS数组

创建数组

  • 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为返回值的初始值
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 由于最近都在freecodecamp上刷代码,运用了很多JavaScript数组的方法,因此做了一份关于JavaS...
    2bc5f46e925b阅读 2,030评论 0 16
  • js中数组是比较常用的对象,同时js中的数组也十分的灵活。一. 创建数组的方式 字面量数组 var arr=[1,...
    饥人谷_廖珍阅读 594评论 0 2
  • 一、数组方法汇总 shift:删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined var ...
    快抓住那个胖子阅读 303评论 0 0
  • 文章如有错误请联系我 anmingzhe@me.com Array.of() 创建数组 参数就是数组参数 Arra...
    安明哲阅读 1,058评论 0 9
  • 数组方法里push、pop、shift、unshift、join、split分别是什么作用 push(val):在...
    王难道阅读 404评论 0 0