[JS] 第十课:数组

依然范特西╮ 提交于 2020-01-24 21:00:53

1. 数组的创建

  1. 通过构造函数创建数组
 var array = new Array();//------>创建一个空数组
 var array = new Array(2);//------>创建一个长度为2的数组
 var array = new Array(1,2,3);//------>创建一个为[1,2,3]的数组
  1. 使用数组字面量创建数组
var array = [];
var array = [1,2,3]; 

⚠️ 一个数组里的元素可以是不同类型。

2. 数组构造函数上的方法

  1. Array.isArray(arg)
    功能:检测传入的参数是否为数组
    返回值:布尔值
Array.isArray([]);  //true
Array.isArray({name:"Tom"});   //false
  1. Array.from(itms[,mapfn[,thisarg]])
    功能:从类似于数组的非数组元素或可迭代对象创建一个数组
    参数:
    -itms:想要转化为数组的为数组对象或可叠戴对象
    -mapfn:回调函数,新数组中的每个元素会执行,数组中的元素变为执行后的返回值,可选。(该函数包含两个参数:item–数组中当前正在处理的元素;index–正在处理的当前元素的索引)
    -thisarg:限定执行mapfn时的this值
    返回值:新的数组
var operator = {
	add:function(n){
		return n+2;
	},
	operator:function(n){
		return this.add(n);
	}
};
array=Array.from([1,2,3],operator.operator,operator);
console.log(array);  //[3,4,5]
  1. Array.of(…items)
    功能:根据输入参数生成新的数组
    参数:items–任意多个参数,按顺序成为新数组中的元素
    返回值:新的数组
var array=Array.of(1,2,3,4);
console.log(array);   //[1,2,3,4]

3. 数组实例上的属性

  1. arr.length
    功能:得到一个数组的长度
var array=[1,2,3];
console.log(array.length);   //3

4. 数组原型上的方法

4.1 更改原数组的方法

  1. Array.prototype.sort(comparefn)
    功能:对数组进行排序
    参数:指定某种顺序进行排序的函数,可选。如果没有指定,则将元素转化为字符串,并按个字符的unicode编码排序。
    返回值:排序后的数组,原数组会被改变。
var scoresList=[
{name:"Tom",score:90},
{name:"Tony",score:60},
{name:"John",score:75},
{name:"Sahra",score:100},
];
var =function byScore(a,b)
{
	return a.score-b.score;
	//返回值为排序依据;按找a,b的顺序传入参数,如果返回值<0则a b; 如果>0则b a; 等于0则ab顺序不变
}
var soertedArray=scoresList.sort(byScore);
console.log(soertedArray);

在这里插入图片描述

  1. Array.prototype.reverse()
    功能:将数组中的元素位置颠倒
var scoresList=[
{name:"Tom",score:90},
{name:"Tony",score:60},
{name:"John",score:75},
{name:"Sahra",score:100},
];
var array = scoresList.reverse();
console.log(array);

在这里插入图片描述

  1. Array.prototype.push(…items)
    功能:讲一个或多个元素添加到数组的末尾
    返回值:添加元素后的新length值

  2. Array.prototype.unshift(…items)
    功能:讲一个或多个元素添加到数组的开头
    返回值:添加元素后的新length值

  3. Array.prototype.pop()
    功能:删除数组中的最后一个元素
    返回值:从数组中删除的最后一个元素,如果原数组为空则返回undefined

  4. Array.prototype.shift()
    功能:删除数组中的第一个元素
    返回值:从数组中删除的第一个元素,如果原数组为空则返回undefined

  5. Array.prototype.splice(index,deleteCount,…items)
    功能:在任意位置删除或添加元素
    参数:
    -index:修改的开始位置
    -deleteCount:要移除的数组元素的个数,整数,可选
    -…items:要添加进数组的元素,可选
    返回值:被改变后的数组

  6. Array.prototype.fill(value[,start[,end]])
    功能:使用给定的值填充从起始位置到结束位置之间的全部元素
    参数:
    -value:用来填充数组元素的值
    -start:起始索引,可选,默认为0
    -end:结束索引,可选,默认值是数组长度
    返回值:修改后的数组

  7. Array.prototype.copyWith(target[,start][,end])
    功能:浅复制数组的一部分到同一个数组中的另外一个位置
    参数:
    -target:复制的目标位置
    -start:复制的起始位置,可选
    -end:复制的结束为止,可选
    返回值:被修改后的数组

4.2 不会更改原数组的方法

  1. Array.prototype.slice(start[, end])
    功能:截取数组中开始位置到结束位置的元素(不包括结束位置),浅拷贝生成新的数组
    ⚠️前拷贝:只拷贝了引用,所以对浅拷贝进行更改原数据会被更改。
    参数:
    -start:开始提取的位置,整数
    -end:结束提取的位置,可选
    返回值:包含提取元素的新数组

  2. Array.prototype.concat(…arguments)
    功能:合并两个或多个数组

  3. Array.prototype.join(separator)
    功能:将数组中的元素连接到一个字符串里
    参数:separator–元素之间的分隔符,可选,默认“,”
    返回值:元素拼接的字符串
    在这里插入图片描述

  4. Array.prototype.forEach(callBackfn[,thisarg])
    功能:遍历整个数组,对每个元素执行callBack函数
    参数:
    -callBackfn:数组中每个元素执行的函数(该函数包含三个参数:currentvalue–数组中正在处理的当前元素;index–数组中正在处理的当前元素的索引;array:正在操作的数组)
    -thisarg:callBackfn函数里的this指向,可选
    返回值:无

  5. Array.prototype.map(callBackfn[,thisarg])
    在这里插入图片描述

  6. Array.prototype.reduce(callbackfn[,initialValue])
    在这里插入图片描述
    [例]
    在这里插入图片描述

  7. Array.prototype.reduceRight(callbackfn[,initialValue])
    与Array.prototype.reduce基本相同,唯一区别是此方法从右边开始累加

  8. Array.prototype.indexOf(searchElement[,fromIndex])
    在这里插入图片描述

  9. Array.prototype.lastIndexOf(searchElement[,fromIndex])

  10. Array.prototype.includes(searchElement[,fromIndex])
    在这里插入图片描述

  11. Array.prototype.find(predict[,thisarg])
    在这里插入图片描述
    [例]
    在这里插入图片描述

  12. Array.prototype.findIndex(predict[,thisarg])
    与Array.prototype.find(predict[,thisarg])基本上相同,唯一的区别是此方法的返回值是找到元素的索引,如果没有找到则返回-1.

  13. Array.prototype.filter(callback[,thisarg])
    在这里插入图片描述
    [例]
    在这里插入图片描述

  14. Array.prototype.every(callbackfn[,thisarg])
    在这里插入图片描述
    ⚠️ 只有当数组中的所有元素都通过了测试才会返回true,否则为false。

  15. Array.prototype.some(callbackfn[,thisarg])
    与Array.prototype.every(callbackfn[,thisarg])基本上一样,唯一的不同是此方法只需一个元素通过测试即可返回true。

  16. Array.prototype.entries()
    在这里插入图片描述
    [例]
    在这里插入图片描述
    ⚠️ 当迭代到末尾时{value:undefined, done:true}

  17. Array.prototype.Keys()
    在这里插入图片描述
    [例]
    在这里插入图片描述
    ⚠️ value:0代表这一元素的索引值
    ⚠️ 当迭代到末尾时{value:undefined, done:true}

  18. Array.prototype.values()
    在这里插入图片描述
    [例]
    在这里插入图片描述
    ⚠️ Chrome浏览器中不支持此方法

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!