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 数组链接concat,和数组转字符串join,字符串转数

    js 数组链接用concat js 数组转成字符串 js 字符串转数组

  • js数组题目

    js面试题 js数组 一、按要求分割数组 将"js,数组,分类"字符串数组以/分割 for循环累加 join()把...

  • js 数组

    js 中数组的长度发生改变,数组就真的发生改变,快速清空数组就让数组长度为0js 数组的方法

  • JS数组以及数组变换

    有关数组 数组对象——一种特殊的对象JS其实没有数组,只使用对象来模拟数组 典型数组和JS数组的区别 典型数组 元...

  • 数组检测

    检测是否是数组: 数组转字符串: 字符串转换数组: js对象转换成js字符串: js字符串转换成js对象:

  • 概念集合

    JS中的数组和Arrary的区别 js中的数组就是array对象

  • JS 数组

    JS 数组是一种特殊的对象,不是真正的数组,它只##是通过对象来模拟数组。 JS 数组的定义 let arr = ...

  • javaScript的数组

    js中没有数组类型 js 中数组是以内置对象的形式存在 数组定义01 var attr = new Array('...

  • 数组

    数组的定义: js:存储多个相同类型数据 ,有序的数据;php数组 1,:索引数组,同js;声明:$arrName...

  • js笔记

    js数组 删除某个元素 js数组是否含有某个元素 判断value为undefined cookie操作

网友评论

      本文标题:JS数组

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