javascript引用类型 ——Array类型

ECMAscript数组的每一项都可以保存任何类型的数组, 就是说,数组的第1个位置来保存字符串,第2个位置 来保存对象,第3个位置来保存对象。

一、创建Array

创建数组有2种形式。第一种是构造函数:

        var colosr = new Array();
        var colosr = new Array(20);
        var colosr = new Array("red","blue","black");

也可省去new操作符:

        var colors = Array(3);
        var names = Array("Greg");

创建 数组的第2种方式是 使用数组字面量表示 法:

        var colors = ['red','blue','black'];//创建一个包含3个字符串的数组
        var names = []; //创建一个空数组
        var values = [1,2, ]; //不要这样!这样会创建一个包含2或3项的数组
        var values = [ , , , , , ] //不要这样!会创建一个包含5或6的空数组

 在读取和设置数组的值时,要使用方括号并提供相应值的基于0的数字索引:

        var colors = ["red", "blue", "black"]; //定义一个字符串数组
        alert(colors[0]); //显示第一项 
        colors[2] = "green"; //修改第三项
        colors[3] = "purple"; //新增第四项
        console.log(colors); //colors =  ["red", "blue", "green", "purple"]
        colors.length; //4

数组的length属性很有特点--它不是只 读的。

        var colors = ["red", "blue", "black"]; //定义一个字符串数组
        colors.length = 2; //改变数组长度为2
        console.log(colors[2]); //undefined
        
        colors.length = 4 //改变数组长度为2
        console.log(colors[3]); //undefined

利用length 属性也可以方便地在数组末尾添加新项:

        var colors = ["red", "blue", "black"]; //定义一个字符串数组
        colors[colors.length] = 'green'; //在位置3添加一个颜色green
        colors[colors.length] = 'gray'; //再在位置4添加一个颜色gray
        console.log(colors); // ["red", "blue", "black", "green", "gray"]

由于数组的最后一项始终是length-1,因此下一个就是length,每当末尾添加一项后,其length属性都会自动更新。二

        var colors = ["red", "blue", "black"]; //定义一个字符串数组
        colors[99] = 'brown'; //brown
        console.log(colors.length); //100

二、检测数组 

if(value instanceof Array){
            //对数组执行某些操作
        }

EMCAScript5新增了Array.isArray()方法。这 个方法的目的是最终确定某个值到底是不是数组:

if(Array .isArray(vallue)){
            //对数组执行某些操作
        }

 三、转换方法

toLocaleString();

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

valueOf();返回的还是数组

var colors = ['red', 'black', 'blue'];
console.log(colors.join(",")); //red,black,blue
console.log(colors.join('||')); //red||black||blue

四、栈方法

栈是一种LIFO 后进先出的数据结构。

栈中项的插入和 移除,只以生在顶部。

push(),pop()

        var colors = new Array();
        var count = colors.push('red', 'black');
        console.log(count); //2
        count = colors.push('blue');
        console.log(count) //3
        var item = colors.pop(); //取得最后一项
        console.log(item); //blue
        console.log(colors.length); //2

五、队列方法

LIFO后进先出。队列在列表的末端添加 项,从列表的前端移除项,

shift(),push()

        var colors = new Array();
        var count = colors.push('red', 'black');
        console.log(count); //2

        count = colors.push('black');
        console.log(count); //3

        var item = colors.shift(); //取得第一项
        console.log(item); //red
        console.log(colors.length); //2

unshift()用途与shift相反。它能在数组前端添加任意个项并返回新数组的长度。

        var colors = new Array();
        var count = colors.unshift('red', 'green'); //推入两项
        console.log(count); //2

        count = colors.unshift('black');
        console.log(count); //3

        var item = colors.pop(); //取得第一项
        console.log(item); //green
        console.log(colors.length); //2

六、重排序方法

reverse() 降序

sort() 升序

        var values = [1, 2, 3, 4, 5];
        values.reverse();
        console.log(values); //[5, 4, 3, 2, 1]


        values.sort();
        console.log(values); //[1, 2, 3, 4, 5]

七、操作方法

concat():可以基于当前数组中的所有项创建一个新数组

slice():可以接受一或两个参数,即要返回项的起始和结束位置。

以下为位置方法:

indexOf():从数组的 开头即位置0开始向后查找

lastIndexOf():则从 数组的末尾开始向前查找

以下为迭代方法:

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

fitler():对数组中的每一项运行给定函数,返回该函数会返回true的项组成的数组

forEach():对数组中的每一项运行给定函数,这个方法没有返回值

map():对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组

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

以下为归并方法:

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

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

都接收4个参数:前一个值,当前值,项的索引和数组对象

concat()例子:

        var colors = ['red', 'black']
        var colors2 = colors.concat(['green', 'purple', 'blue']);

        console.log(colors); //["red", "black"]
        console.log(colors2); //["red", "black", "green", "purple", "blue"]

slice()有3种方式:

(1)删除:可以删除任意数量的项。指定2个参数:要删除的第一项的位置和要删除的项数。例如:splice(0,2)会删除数组中的前两项

(2)插入:可以向指定位置插入任意数量的项,只需要指定3个参数:起始位置,0(要删除的项数)和要插入的项。例如:splice(2,0,"red","blue")

(3)替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需要 指定3个参数:起始位置,要删除的项数和要插入的任意数量的项。例如:splice(2,1,"red","blue")会删除当前数组位置2的项,然后再从位置2开始插入字符串

slice()例子:

        var colors = ['red', 'blue', 'green'];
        var removed = colors.splice(0, 1); //删除第一项
        console.log(colors); //["blue", "green"]
        console.log(removed); //["red"]

        removed = colors.splice(1, 0, 'yellow', 'black'); //从位置一开始插入两项
        console.log(colors); //["blue", "yellow", "black", "green"]
        console.log(removed); // 空数组

        removed = colors.splice(1, 1, 'red', 'purple');
        console.log(colors); // ["blue", "red", "purple", "black", "green"]
        console.log(removed); //["yellow"]

indexOf(),lastIndexOf()例子:

        var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
        console.log(numbers.indexOf(4)); //3
        console.log(numbers.indexOf(8)); //7
        console.log(numbers.lastIndexOf(8)); //7 ?
        console.log(numbers.lastIndexOf(9)); //8 ?

归并方法的例子:

        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); //false

        var someResult = numbers.some(function(item, index, array) {
            return (item > 2);
        })
        console.log(someResult); //true
        var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
        var filterResult = numbers.filter(function(item, index, array) {
            return (item > 2);
        })
        console.log(filterResult); //[3, 4, 5, 4, 3]
        var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
        var mapResult = numbers.map(function(item, index, array) {
            return item * 2;
        })
        console.log(mapResult); //[2, 4, 6, 8, 10, 8, 6, 4, 2]

归并方法:

使用reduce()方法可以执行求数组中所有值之和的 操作

        var numbers = [1, 2, 3, 4, 5];
        var sum = numbers.reduce(function(prev, cur, index, array) {
            return prev + cur;
        })
        console.log(sum); //15
        var numbers = [1, 2, 3, 4, 5];
        var sum = numbers.reduceRight(function(prev, cur, index, array) {
            return prev + cur;
        })
        console.log(sum); //15

猜你喜欢

转载自blog.csdn.net/weixin_42659625/article/details/82632355