数据类型总结——Array(数组类型)

原文

  简书原文:https://www.jianshu.com/p/1e4425383a65

大纲

  前言
  1、Array数组类型的相关概念
  2、创建数组的基本方式有两种
  3、检测某个变量是否是数组的方式
  4、数组的遍历:for...in语句
  5、数组的常用方法

前言

  数据类型是每一种语言都需要掌握的内容,掌握每一种数据类型的使用是掌握这门语言必不可少的。而我也对数据类型写了一系列的博客,其中包含了对某一数据类型的概念的认识和理解以及常使用的方法。以下就是我对Array类型的一些认识和理解,希望能对读者有所帮助。并且这是关于ES6之前的一篇,之后还会有一篇关于ES6对数组的新增的知识的总结。

1、Array数组类型的相关概念

  1、数组是一种特殊的变量,他由多个数组元素构成,可以保存多个不同类型的数据。数组的存在是为了解决一个变量只能存储一个数据的局限,使用数组可以保存多个数据项。
  2、数组的声明不同于变量的声明,需要通过new Array()来创建数组。
  3、每个数组元素的索引是唯一的,通过索引就可以为指定的数组元素赋值或访问指定的数组元素。
  4、ECMAScript中的数组是数据的有序列表,不同于其它语言,ECMAScript数组的每一项可以保存不同的任何类型的数据。ECMAScript数组的大小是可以动态调整的,即可以随着数据的添加自动增长以容纳新增数据。

var colors = ["red","bule","green"];
colors[99] = "black";
console.log(colors.length);//100
console.log(colors[98]);//undefined
console.log(colors[99]);//black

  5、JavaScript只支持一维数组,而不存在多维数组。JavaScript允许把一个数组的元素声明为一个新的数组,从而模拟出多维数组。

var personel = new Array();
personel[0] = new Array();
personel[0][0] = "Name0";
personel[0][1] = "Age0";
personel[0][2] = "Address0";

personel[1] = new Array();
personel[1][0] = "Name1";
personel[1][1] = "Age1";
personel[1][2] = "Address1";

personel[2] = new Array();
personel[2][0] = "Name2";
personel[2][1] = "Age2";
personel[2][2] = "Address2";

console.log(personel);

2、创建数组的基本方式有两种

  1、使用Array构造函数

var colors = new Array();
var colors = new Array(20);
var colors = new Array("red","blue","green");

  2、使用数组字面量表示法,与对象一样,在使用数组字面量表示法时,也不会调用Array构造函数。

var colors = ["red","blue","green"];
var colors[colors.length] = "black";

3、检测某个变量是否是数组的方式

  1、使用instanceof操作符(当使用框架的时候,在不同的框架中的全局执行环境不同可能会有问题)

var colors = ["red","bule","green"];
var isArr = colors instanceof Array;
console.log(isArr);

  2、ES5新增的Array.isArray()方法,用于确定某个值到底是不是数组

var isArr2 = Array.isArray(colors);
console.log(isArr2);

4、数组的遍历:for...in语句

  在js中,数组不是数据类型,数组的数据类型其实就是对象。Js中的For.....in语句可以实现对一个对象的所有属性的遍历。也可以使用for...in语句实现对一个数组的所有元素的遍历for( var i in array ){}。原理:数组中有几个元素,for..in语句就循环执行多少次。每次执行时,将当前数组元素的下标存放到变量i中

var row = ['zhangsan','lisi','wangwu','xiaoqiang'];

for (var i in row){
     //document.write(i + ':' + row[i] + '<br>');
     console.log(row[i]);
} 
//zhangsan
//lisi
//wangwu
//xiaoqiang

5、数组的常用方法

5.1、栈方法:push()和pop()

  ECMAScript数组提供了一种让数组的行为类似于栈的操作方法(栈:一种可以限制插入和删除的数据结构,LIFO:last-In-First-Out后进先出,在栈中项的插入叫做推入,移除叫做弹出)
  ECMAScript数组提供了push()和pop()方法,以便实现类似栈的行为

var colors = new Array();
var count = colors.push("red","green");
console.log(count);//2//push方法推入值并返回数组的长度
count = colors.push("black");
console.log(count);//3
var item = colors.pop();//pop方法弹出数组的最后进入的项,并返回该项
console.log(item);//black
console.log(colors.length);//2

5.2、队列方法:push()和shift()

  队列数据结构的访问规则是FIFO(First-In-First-Out先进先出,队列在列表的末端添加项,从列表的前端移除项)
  通过push向数组末端添加项,通过shift()方法取得数组前端项,结合使用便可以像使用队列一样使用数组

var colors = new Array();
var count = colors.push("red","green");
console.log(count);//2//push方法推入值并返回数组的长度
count = colors.push("black");
console.log(count);//3
var item = colors.shift();//shift方法弹出数组的第一项,并返回该项
console.log(item);//red
console.log(colors.length);//2

5.3、unshift()方法

  ECMAScript还为数组提供了一个unshift()方法。利用unshift()方法能在数组前端添加任意个项并返回数组长度。
  利用pop()方法可以取得数组末端的项。
  结合unshif()方法和pop()方法可以从相反的方向来模拟队列,即在数组的前端添加项,从数组末端移除项。

5.4、排序方法:reverse()和sort()

  数组中已经存在两个可以直接用来重新排序的方法:reverse()和sort()。

  reverse():该方法会反转数组的顺序

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

  sort()方法按升序排列数组项,即最小的值位于最前面,最大的值排在最后面。为了实现排序,sort()方法会调用每个数组的toString()转型方法,然后比较得到字符串,以确定如何排序,即使数组中的每一项都是数值,sort()方法比较的也是字符串。

var values = [0,1,5,10,15];
values.sort();
console.log(values);//(5) [0, 1, 10, 15, 5]

5.5、concat()方法

  concat()方法用于将一个项或多个项推入数组中,并返回这个合成之后的数组。

var colors = ["red","bule","green"];
var colors2 = colors.concat("yellow",["black","brown"]);
console.log(colors);//(3) ["red", "bule", "green"]
console.log(colors2);//(6) ["red", "bule", "green", "yellow", "black", "brown"]

5.6、slice()方法

  slice()方法用于从数组中导出一个或多个项从而返回一个由这些项组成的新数组。

var colors = ["red","bule","green","yellow","purple"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,4);
console.log(colors);//(5) ["red", "bule", "green", "yellow", "purple"]
console.log(colors2);//(4) ["bule", "green", "yellow", "purple"]
console.log(colors3);//(3) ["bule", "green", "yellow"]

5.7、splice()方法

  splice()方法用于对数组中一个或多个项进行删除、插入、替换的操作。

//1、用作删除数组中元素
var colors = ["red" , "green","blue"];
var removed = colors.splice(0,1);//删除数组中0开始的第一项
console.log(colors);//(2) ["green", "blue"]
console.log(removed);//["red"]

//2、用于插入数组元素
var colors = ["red" , "green","blue"];
var inserted = colors.splice(0,0,"yellow","orange");//在数组中0的位置上插入两项
console.log(colors);//(5) ["yellow", "orange", "red", "green", "blue"]
console.log(inserted);//[]

//3、替换数组中元素
var colors = ["red" , "green","blue"];
var inserted = colors.splice(0,1,"yellow","orange");//删除数组上的0开始的1项并插入两项
console.log(colors);//(4) ["yellow", "orange", "green", "blue"]
console.log(inserted);//["red"]

5.8、位置方法:查找元素所在位置——indexOf()和lastIndexOf()

  ECMAScript为数组添加了两个位置方法:indexOf()和lastIndexOf()
  这两个方法都接收两个参数:要查找的项和表示查找起点位置的索引(可选)
  这两个方法返回的都是要查找的项在数组中的位置,没找到返回-1,查找的过程中使用的严格的全等方式比较
  indexOf()是从首位开始查找,lastIndexOf()是从末尾开始往回查找

5.9、迭代方法:every()、some()、filter()、map()、forEach()

  ECMAScript为数组定义了5个迭代方法。
  每个方法都接收两个参数:要在每一项上运行的函数和运行该函数的作用域对象(可选的)——影响this的值
  传入这些方法中的函数会接收三个参数:数组项的值、该项在数组中的位置和数组对象本身。
  迭代方法都需要传入每一项上运行的函数,即需要对每一项进行操作的函数,这样才能知道对数组的每一项进行什么操作。

  every():若数组的每个项都满足条件,返回true,若有一项不满足,返回false

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

  some():若数组的某一项满足条件,返回true,若没有一项满足条件,则返回false

var numbers = [1,2,3,4,5,4,3,2,1];
var someResult = numbers.some(function(item,index,array){
    return(item > 2);
    //return(index > 20);//false
});
console.log(someResult);//true

  filter():将满足条件的项过滤出来

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

  map():对数组中的每个项都进行同样的操作

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);//(9) [2, 4, 6, 8, 10, 8, 6, 4, 2]

  forEach():对数组中的每个项都进行同样的操作,不同于map(),map()方法是拷贝一个数组副本,然后对数组中的每个元素进行操作,但是forEach()则是对数组本身进行操作。

var numbers = [1,2,3,4,5,4,3,2,1];
var forEachResult = numbers.forEach(function(item,index,array){
//1     item = item * 2;//1
//2     array[index] = array[index] * 2;//2
    array[index] = item * 2;//3
//     console.log(item*2 === array[index]);//true
//     console.log(array[index]);
});
console.log(forEachResult);//undefined
console.log(numbers);
//1、(9) [1, 2, 3, 4, 5, 4, 3, 2, 1]
//2、(9) [2, 4, 6, 8, 10, 8, 6, 4, 2]
//3、(9) [2, 4, 6, 8, 10, 8, 6, 4, 2]

5.10、缩小方法:reduce()和reduceRight()

  ECMAScript 5中新增了两个缩小数组的方法:reduce()和reduceRight()。这两个方法都会迭代数组的所有项,然后构建一个最终返回的值。reduce()方法从数组的第一项开始遍历,reduceRight()从数组最后一项开始遍历。这两个方法都接收两个参数:一个在每一项上调用的函数和作为缩小基础的初始值(可选)。传给reduce()和reduceRight()的操作函数接收4个参数:前一个值、当前值、项的索引、数组对象。

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

  

猜你喜欢

转载自www.cnblogs.com/shcrk/p/9276989.html