JavaScript内置对象(一):Array数组

JavaScript内置对象(二):String字符串
JavaScript内置对象(三):Math算数
JavaScript内置对象(四):Date时间

Array数组

数组对象的作用是:使用单独的变量名来存储一系列的值。

创建数组
1.使用Array构造函数

语法:new Array();

小括号()说明:(1)预先知道数组要保存的项目数量;(2)向Array构造函数中传递数组应包含的项。

var arr1 = new Array(); //创建一个空的数组
arr1[0] = "I"; 
arr1[1] = "love";
arr1[2] = "color";
var arr2 = new Array(10); //创建一个长度为10的空数组,里边有10个空的数组元素
var arr3 = new Array(0,1,2,3,4,5,6,7,8,9);//创建一个数组,里边10个元素0-9;
2.使用数组字面量表示法

有一对包含数组的方括号[]表示,多个数组项之间以逗号隔开。

var arr = [0,1,2,3,4,5,6,7,8,9];
属性
1.array.length 获取数组array的长度,返回number。
  • 数组遍历
var arr = ["red","orange","yellow","green","blue"];
for(var i = 0;i < arr.length;i++){
	console.log(arr[i]);
}
// 也可以使用forEach进行遍历,它每个数组元素调用一次函数(回调函数)。
arr.forEach(function (item, index, array) {
    console.log(item, index);
});
2.array.constructor 返回对创建此对象的数组函数的引用。

返回值是函数的引用,不是函数名:

JavaScript 数组 constructor 属性 返回 function Array() { [native code] };
JavaScript 数字 constructor 属性 返回 function Number() { [native code] };
JavaScript 字符串 constructor 属性 返回 function String() { [native code] }。

3.array.prototype 给数组对象添加属性和方法。
方法
数组的转换方法和重排序方法

1.join(separator) 将数组中元素合并为字符串,separator表示分隔符。如省略参数,则默认“,”,返回字符串。

var arr = new Array(3); 
arr[0] = "I"; 
arr[1] = "love";
arr[2] = "color";

console.log(arr.join()); //I,love,color
console.log(arr.join(".")); //I.love.color
console.log(arr.join("-")); //I-love-color

2.reverse() 颠倒数组中元素的顺序,反向排列,返回数组。

var arr = new Array(3); 
arr[0] = "I"; 
arr[1] = "love";
arr[2] = "color";

console.log(arr); //[ "I", "love", "color" ]
console.log(arr.reverse()); //[ "color", "love", "I" ]

3.sort(compare Function) 使数组中的元素按照一定的顺序排列,若不指定方法函数,则按Unicode码顺序排列。会改变原数组

//定义一个函数用来给数组中的元素排序
function sortNumber(a,b){
	return a - b
}

var arr = new Array(6);
arr[0] = "10";
arr[1] = "5";
arr[2] = "40";
arr[3] = "25";
arr[4] = "1000";
arr[5] = "1";
console.log(arr); //[ "10", "5", "40", "25", "1000", "1" ]
console.log(arr.sort()); // [ "1", "10", "1000", "25", "40", "5" ]
console.log(arr.sort(sortNumber)); //[ "1", "5", "10", "25", "40", "1000" ]
console.log(arr); //[ "1", "5", "10", "25", "40", "1000" ]

4.toString() 将数组所有元素返回一个字符串,其间用逗号分隔。

var arr = new Array(3); 
arr[0] = "I"; 
arr[1] = "love";
arr[2] = "color";
console.log(arr.toString()); //I,love,color
数组的操作方法

1.concat(array1,arrayn)将两个或两个以上的数组值连接起来,合并后以数组形式返回结果,不会改变原数组

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

var b = [6,7,8];
var c = [9,0];
console.log(a.concat(b,c)); //[1, 2, 3, 6, 7, 8, 9, 0]
console.log(a); //[1, 2, 3]

2.splice 会改变原数组

var arr = ["red","orange","yellow","green","blue"];
console.log(arr.splice(1,2)); //["orange", "yellow"]
console.log(arr); //["red", "green", "blue"]

console.log(arr.splice(1,0,"pink","black")); //[]
console.log(arr); // ["red", "pink", "black", "green", "blue"]

console.log(arr.splice(2,2,"white")); // ["black", "green"]
console.log(arr); // ["red", "pink", "white", "blue"]

console.log(arr.splice(1)); //["pink", "white", "blue"]
console.log(arr);  //["red"]

(1)splice(index,count)
用于删除从index开始的零个或多个元素,返回被删除的元素的数组
(2)splice(index,0,item1,…,itemX)
用于在指定位置插入值,返回数组。
(3)splice(index,count,item1,…,itemX)
用于在指定位置插入值且同时删除任意数量的项,返回从原始数组中删除的项(若没有删除任何项,则返回空数组)。

补充:index表示起始位置的索引值;0和count表示要删除的项数,如果设置为0,则不会删除项,如果不设置,则删除从index开始的所有值。

3.slice(start, end) 从已有的数组中返回选定的元素,返回数组。不会改变原数组

var arr = ["red","orange","yellow","green","blue"];
console.log(arr.slice(1,2)); //["orange"]
console.log(arr); //["red", "orange", "yellow", "green", "blue"]

console.log(arr.slice(-3,-1)); // ["yellow", "green"]
console.log(arr); // ["red", "orange", "yellow", "green", "blue"]

console.log(arr.slice(2)); // ["yellow", "green", "blue"]
console.log(arr); // ["red", "orange", "yellow", "green", "blue"]

console.log(arr.slice(1,-2)); // ["black", "green"]
console.log(arr); // [ "red", "orange", "yellow", "green", "blue" ]

(1)start和end指的是数组中的索引值;截取从start到end(不包括该元素)的元素,即start到end-1的元素。
(2)start如果为负数,那么它规定从数组尾部开始算起的位置。
(3)如果slice()方法中有一个参数为负数,则用数组长度加上该数来确定相应位置。

补充:如果不设置end,则选取从index开始到结尾的所有值。

数组的栈方法(也叫添加删除数组元素方法,会使原数组产生改变)

1.pop() 删除数组中的最后一个元素并返回被删除的元素

var arr = new Array(3); 
arr[0] = "I"; 
arr[1] = "love";
arr[2] = "color";

document.write(arr); //I,love,color
document.write("<br />");
document.write(arr.pop()); //color
document.write("<br />");
document.write(arr); //I,love

2.push(参数1,参数2,…,参数n) 将参数按顺序添加到数组的末尾,并且返回新的数组长度值

var arr = new Array(3); 
arr[0] = "I"; 
arr[1] = "love";
arr[2] = "color";

document.write(arr); //I,love,color
document.write("<br />");
document.write(arr.push("red")); //4
document.write("<br />");
document.write(arr); //I,love,color,red

3.shift() 删除数组中的第一个元素并返回被删除的元素

var arr = new Array(3); 
arr[0] = "I"; 
arr[1] = "love";
arr[2] = "color";

document.write(arr); //I,love,color
document.write("<br />");
document.write(arr.shift()); //I
document.write("<br />");
document.write(arr); //love,color

4.unshift(参数1,参数2,…,参数n) 将参数按顺序添加到数组的开头,并且返回该数组的新长度

var arr = new Array(3); 
arr[0] = "I"; 
arr[1] = "love";
arr[2] = "color";

document.write(arr); //I,love,color
document.write("<br />");
document.write(arr.unshift("red")); //4
document.write("<br />");
document.write(arr); //red,I,love,color
数组的索引方法(也叫数组的位置方法)

1.indexOf() 数组中查找给定元素的第一个索引 ,如果存在返回索引号,如果不存在,返回-1;

var str = "Hello world!";
document.write(str.indexOf("Hello") + "<br />"); //0
document.write(str.indexOf("World") + "<br />"); //-1
document.write(str.indexOf("world")); //6

2.lastindexOf() 在数组中的最后一个索引, 如果存在返回索引号,如果不存在则返回-1;

var str = "Hello world!";
document.write(str.lastindexOf("Hello") + "<br />"); //0
document.write(str.lastindexOf("World") + "<br />"); //-1
document.write(str.lastindexOf("world")); //6

数组去重

var arr = [1,2,3,3,3,5,1,6,2,4]
var newArr = [];
for (var i = 0; i < arr.length; i++) {
    if (newArr.indexOf(arr[i]) == -1) {
        newArr[newArr.length] = arr[i];
    }
}
数组的检索方法

1.isArray()判断传入的值是否是一个数组
2.includes() 判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。

var arr = ["red","orange","yellow","green","blue"];
console.log(arr.includes("red")); // true
console.log(arr.includes("black")); // false

3.find()返回数组中满足指定函数的第一个元素的值。如果不存在则返回 undefined。

var a = [1,2,3,4,5,6,7,8,9];
console.log(a.find(function(item){return item < 5}));; // 1
console.log(a.find(function(item){return item < 0}));; // undefined

4.every()检测数组中的每个元素是否都符合某回调函数,返回布尔值。如果全部符合则返回true,一旦有一个不符合则返回false。

var a = [1,2,3,4,5,6,7,8,9];
console.log(a.every(function(item){return item > 5}));; // false
console.log(a.every(function(item){return item > 0}));; // true

若收到一个空数组,此方法在一切情况下都会返回 true。

5.some()检测数组中的每个元素是否存在一个元素符合某回调函数,返回布尔值。只要有一个符合就返回true,只有全部不符合才返回false。

var a = [1,2,3,4,5,6,7,8,9];
console.log(a.some(function(item){return item < 5}));; // true
console.log(a.some(function(item){return item < 0}));; // false

若收到一个空数组,此方法在一切情况下都会返回 false。

6.filter检测数组中的每个元素是否符合某回调函数,并将符合的元素返回到一个新数组中,如果都不符合则返回一个空数组

var a = [1,2,3,4,5,6,7,8,9];
console.log(a.filter(function(item){return item < 5}));; // [ 1, 2, 3, 4 ]
console.log(a.filter(function(item){return item < 0}));; // []

7.map()创建一个新数组,让该数组中的每个元素调用指定函数后返回到这个新数组中,不会对原数组进行改动

var a = [1,2,3,4,5,6,7,8,9];
console.log(a.map(function(item){return item*2}));; // [ 2, 4, 6, 8, 10, 12, 14, 16, 18 ]
console.log(a.map(function(item){return item+6}));; // [ 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
console.log(a); //[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

8.reduce类似累加器,将数组各元素相加之和,不影响原数组

var a = [1, 2, 3, 4, 5];
console.log(a.reduce((total, currentValue) => {
	return currentValue + total;
})); // 15

注意:使用reduce经常会遇到的一个错误,准确的来说是空数组使用reduce时经常会遇到的一个错误:Reduce of empty array with no initial value

我们来看他的语法array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
这里注意他的参数initialValue,表示传递给函数的初始值
若数组为空数组,可以像下面这么写,写上initialValue参数

var b = []
console.log(b.reduce((total, currentValue) => {
	return currentValue + total;
},0)); // 0
其他方法

toSource() 返回该对象的源代码
toLocaleString() 把数组转换为本地数组,并返回结果。
valueOf() 返回数组对象的原始值

补充

delete 删除元素,不改变原数组的长度

var arr = ["red","orange","yellow","green","blue"];
delete arr[0];
console.log(arr[0]); //undefined
console.log(arr); // [empty, "orange", "yellow", "green", "blue"]

ES6的方法

ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值。

**[...new Set(array)]**
const set = new Set([1, 2, 3, 4, 4]);
console.log([...set]); // [1,2,3,4]

const s = new Set();
[2, 3, 5, 4, 5, 2, 2].forEach(x => s.add(x));
console.log([...s]); // [2,3,5,4]

其他参考材料

W3C JavaScript Array 对象
菜鸟教程 JavaScript Array(数组) 对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值