array类型

来源:互联网 发布:网络40禁书百度 编辑:程序博客网 时间:2024/06/05 15:23

创建数组方法

  1. 使用Array构造函数 var color = new Array()
  2. 数组字面量表示法 var color = ["red","blue","green"]

length属性即可读也可写,若将length属性设置大于数组项数的值则新增的每一项都会取得undefined


检测数组

Array.isArray()方法


转换方法

toString():返回由数组中每个值得字符串形式拼接而成的一个以逗号分隔的字符串

var colors = ["red","blue","green"];console.log(colors.toString()); red,blue,green//打印结果

ValueOf():返回的还是数组

var colors = ["red","blue","green"];console.log(colors.valueOf()); ["red", "blue", "green"]//打印结果

join()方法

使用不同的分隔符来构建字符串
参数:用作分隔的字符串

var colors = ["red","blue","green"];console.log(colors.join(","));red,blue,green //打印结果console.log(colors.join("|"));red|blue|green //打印结果

sort()排序方法

data.sort(function (d1,d2) {    return d2 - d1;});

栈方法 (LIFO–Last -In-First-Out)

push()

在栈的结尾推入元素

data.push("black");

pop()

在栈的结尾弹出元素

data.pop("black");

队列方法(FIFO–First-In-First-Out)

shift()

在队列的前端取出元素

data.shift("black");

unshift()

在队列的后端添加元素

data.unshift("black");

push方法可以与shift组合使用,从数组后端进,从前端取
unshift方法可以与pop组合使用,从数组前端进,从后端取


操作方法

concat()方法

基于当前数组中的所有项创建一个新数组,具体说来这个方法会创建一个当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组,若没有传递参数,则它只是复制当前数组并返回副本,若传递的是一个或多个数组,则该方法会将这些数组中的每一项都添加到结果数组中

参数:要添加的项

var colors2 = colors.concat("yellow",["black","brown"])console.log(colors2);["red", "blue", "green", "yellow", "black", "brown"]//返回结果var colors3 = colors.concat();console.log(colors3);["red", "blue", "green"]//返回结果

slice()方法

基于当前的一个或多个项创建数组

参数:返回项的起始开始位置,返回项的结束位置(可选,不包括该位置的项)

只有一个参数时返回起始指定位置到数组末尾,参数为负数时则用数组长度加长该数来确定相应的位置

var colors = ["red","green","blue","yellow","purple"];var colors1 = colors.slice(1);var colors2 = colors.slice(1,4);console.log(colors1);console.log(colors2); ["green", "blue", "yellow", "purple"] ["green", "blue", "yellow"]var colors4 = colors.slice(-2,-1);var colors5 = colors.slice(3,4);console.log(colors4);console.log(colors5); ["yellow"] ["yellow"]

注:该方法不会影响原始数组

splice()方法

向数组的中部插入项,使用方法有如下3种

  1. 删除:可以删除任意数量的项,只需指定两个参数:要删除的第一项的位置和要删除的项数,例如,splice(0,2)会删除数组中的前两项
  2. 插入:可以向指定位置插入任意数量的项,只需提供三个参数:起始位置、0(要删除的项数)和要插入的项。在起始位置之前插入,如果要插入多个项,可以再传入第四、第五,以至任意多个项,例如,splice(2,0,"red","green")会在当前数组的位置2开始插入字符串"red","green"
  3. 替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定3个参数:起始位置、要删除的项数和要插入的项数。插入的项数不必与删除的项数相等,例如,splice(2,1,"red","green")会删除当前数组位置2的项,然后再从位置2开始插入字符

splice()方法始终都会返回一个数组,该数组中包含从原始数组中删除的项(如果没有删除项,则返回一个空字符串),将完成的数组返回给调用的数组

var colors = ["red","green","blue"];var removed = colors.splice(0,1);console.log(colors);console.log(removed);VM1054:3 ["green", "blue"]VM1054:4 ["red"]removed = colors.splice(1,0,"yellow","orange");console.log(colors);console.log(removed);VM1143:2 ["green", "yellow", "orange", "blue"]VM1143:3 []removed = colors.splice(1,1,"red","purple");console.log(colors);console.log(removed);VM1243:2 ["green", "red", "purple", "orange", "blue"]VM1243:3 ["yellow"]

位置方法

indexOf()方法

从数组的开头(位置0)开始向后查找,返回要查找的项在数组中的位置,没有找到返回-1,使用全等符号查找

参数:要查找的项,表示查找起点位置的索引(可选的)

var numbers = [1,2,3,4,5,4,3,2,1];console.log(numbers.indexOf(4)); 3//找的是第一个的位置console.log(numbers.indexOf(4,4)); 5console.log(numbers.indexOf(3)); 2

lastIndexOf()方法

从数组的末尾向前查找,返回要查找的项在数组中的位置,没有找到返回-1,使用全等符号查找

参数:要查找的项,表示查找起点位置的索引(可选的)

var numbers = [1,2,3,4,5,4,3,2,1];console.log(numbers.lastIndexOf(3)); 6//找的是最后一个相同的位置,从后往前计数console.log(numbers.lastIndexOf(4)); 5console.log(numbers.lastIndexOf(4,4)); 3

迭代方法

参数:要在每一项上运行的函数,运行该函数的作用域对象(可选),影响this的值

传入这些方法中的函数会接收三个参数:数组项的值、该项在数组中的位置、数组对象本身

every()方法

对数组中的每一项运行给定的函数;如果该函数对每一项都返回true,则返回true

var numbers = [1,2,3,4,5,4,3,2,1];var everyResult = numbers.every(function(item,index,array){    return (item > 2); });console.log(everyResult);VM1694:5 false

some()方法

对数组中的每一项运行给定的函数;如果该函数对任一项返回true,则返回true

var someResult = numbers.some(function(item,index,array){    return (item > 2);});console.log(someResult);VM1861:1 true

filter()方法

对数组中的每一项运行给定的函数;返回该函数会返回true的项组成的数组

var filterResult = numbers.filter(function(item,index,array){    return (item > 2);});console.log(filterResult);VM2041:4 [3, 4, 5, 4, 3]

map()方法

对数组中的每一项运行给定的函数;返回每次函数调用的结果组成的数组

var mapResult = numbers.map(function(item,index,array){    return (item > 2);});console.log(mapResult);VM2209:4 [false, false, true, true, true, true, true, false, false]var mapResult = numbers.map(function(item,index,array){    return (item * 2);});console.log(mapResult);VM2538:4 [2, 4, 6, 8, 10, 8, 6, 4, 2]

forEach()方法

对数组中的每一项运行给定的函数;无返回值,本质上与使用for循环迭代数组一样

var forEachResult = numbers.forEach(function(item,index,array){    return (item > 2);});console.log(forEachResult);VM2389:4 undefined

归并方法

迭代数组中所有的项,然后构建一个最终返回的值

参数:在每一项上调用的函数,作为归并基础的初始值(可选)

传入给它们的函数接收4个参数:前一个值、当前值、项的索引、数组对象,这个函数返回的任何值都会作为第一个参数自动传递给下一项,第一次迭代发生在数组的第二项上,因此第一个参数时数组的第一项,第二个参数是数组的第二项

reduce()方法

从数组的第一项开始,逐个遍历到最后

var values = [1,2,3,4,5];var sum = values.reduce(function(pre,cur,index,array){    return (pre + cur);});console.log(sum);VM3459:5 15

第一次执行回调函数,prev是1,cur是2,第二次执行回调函数pre是3(1加2的结果),cur是3(数组的第三项),这个过程会持续到把数组中的每一项都访问一遍,最终返回结果

reduceRight()方法

从数组的最后一项开始,向前遍历到第一项

var sum = values.reduceRight(function(pre,cur,index,array){    return (pre + cur);});console.log(sum);VM3604:4 15

参考:《JavaScript高级程序设计 第3版》

0 0
原创粉丝点击