JavaScript——数组Array

本文详细介绍了JavaScript数组的基础概念、创建方式、访问元素的方法、数组检测、转换及常用操作方法等核心内容,包括添加/删除元素、排序、拼接、切割、查找、迭代等,并提供了丰富的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(2)数组

□ []
□ 一组数据的集合
□ 数据中的所有数组元素,使用逗号隔开,每一个数组元素数据类型任意
□ 数组长度可以动态修改

□ 1、创建
	® a、数组字面量
		◊ var arr = [];       //空数组
		◊ var arr = [null, undefined, "", false, {name: "lisi"}]
	® b、构造函数
		◊ var arr = new Array();         //空数组
		◊ var arr = new Array("hello", false);           //创建非空数组
		◊ var arr = new Array(10);          //创建一个数组长度为10的数组
		◊ var arr = new Array(10, 20);
		如果参数为1个参数,并且参数类型为number类型,相当于创建一个以number为长度的数组
		如果两个以上参数,相当于将参数作为数组元素创建数组
		
□ 2、访问数组元素
	每一个数组元素都对应一个数组下标(属性名)
	数组下标从0开始(number)
		arr[index]:访问+设置
		var arr = [1, 2,3];
		console.log(arr[0]);

□ 3、检测数组
	function:
		typeof function:function
		
	arr:
		typeof arr:object

	isArray():构造函数(静态方法)
		Array.isArray(arr);
		当前arr是否为Array类型
□ 4、转换数组
	toString()
		arr.toString();          重写了toString()
	join('-')
		将数组转换为string,用'-'分隔每一个数组元素
	
□ 5、方法
	® 添加/删除数组元素
		◊ 栈方法:
			} 先入后出
			pop():
				删除数组元素
				参数:不需要
				返回值:删除的数组元素
			push():
				添加数组元素
				参数:添加的数组项
				返回值:添加后的数组长度
			
		◊ 队列方法:
			} 先入先出
			unshift():
				添加数组元素
				参数:添加的数组项
				返回值:添加后的数组长度
				
			shift():
				删除数组元素
				参数:不需要
				返回值:删除的数组元素
				
	® 排序
		◊ reverse:
			数组反转
			参数:不需要
			返回值:反转之后的数组,并且修改原数组
		◊ sort():
			数组排序
			参数:
				0:
					每一个数组项,相当于会调用toString()之后进行比较,按照字符编码顺序进行比较
					var arr = [1, 5, 3, 0, 11, 6];
				        var result = arr.sort();
				        console.log(result);        //Array(6) [ 0, 1, 11, 3, 5, 6 ]
				        console.log(arr);
				1:
					回调函数
					var result = arr.sort(function(a, b){
				            // console.log(a, b);
				            if(a > b){
				                return 1;   //将a放在b之后
				            }
				            else if(a < b){
				                return -1;  //将a放在b之前
				            }else {
				                return 0;      //a=b不排序
				            }
				        });
				
					
			返回值:排序后的数组(在原数组基础上进行排序)
			
			var arr = [{
				name: 'a',
				age: 20,
				score: 5
			},{
				name: 'b',
				age: 20,
				score: 1
			},{
				name: 'a',
				age: 20,
				score: 2
			}]
			
	® 操作方法(非静态方法)
		◊ concat:
			数组拼接
			(arr, arr1)
		◊ slice():不修改原数组
			数组切割/截取
			参数:
				0:
					返回值:切割后的新数组默认从数组开头开始切割,切割到数组末尾,组成新数组
				1:
					参数:index   开始切割的位置
					返回值:从当前位置开始切割,到数组末尾的新数组
				2:
					begin     end
					返回值:从begin位置开始切割,切割到end位置结束,不包含结束位置
		◊ splice():
			数组切割
			参数:
				0:
					返回值:空数组
				1:
					index:begin
					返回值:从begin位置开始切割,到数组末尾结束,修改原数组
				2:
					begin number
					返回值:从begin位置开始切割,切割number个数组元素所组成的新数组
					number > 剩余数组元素个数——》剩余数组元素
				3:
					begin number "content"
					返回值:
						替换:从begin位置开始,删除number个数组元素,在begin位置插入content
						
						插入:begin 0 'content'
						从begin位置开始,删除0个,在begin位置插入content
		◊ indexOf(key, index):
			查找数组元素
			参数:content index
				通过===进行查找
			返回值:
				找到了返回当前数组元素的下标
				找不到返回-1
				默认从前向后查找,找到一个满足条件的元素返回
		◊ lastIndexOf(key, index):
			查找数组元素
			返回值:
				查找数组元素
				默认从后向前查找
				到不到返回-1
				找到返回当前数组元素的下标
	® 迭代方法
		◊ every():
			判断数组中元素是否每一个都满足条件,都满足条件返回true,只要有一个不满足,返回false
			var result = arr1.every(function(item, index, arr){
		            return item > 4;
		        });
		        console.log(result);
		◊ some():
			判断数组中是否含有满足条件的数组元素,存在一个即返回true,否则返回false
		◊ map():不修改原数组
			对每一个数组元素进行操作,返回值操作后的数组
			var result = arr1.map(function(item, index, arr){
		            return item + 10;
		        });
		        console.log(result);
		        console.log(arr1);
			
		◊ filter():
			数组过滤
			将满足条件的数组元素筛选出来放在一个新数组中
			
		◊ forEach()
			for(){}
			循环体
			index item
			arr.forEach(function(item, index, arr){
		            console.log(item, index);
		        });
			
		参数:
			回调函数  this
			回调函数:
				有几个数组元素,回调函数被执行几次
				参数:item        index      arr
					数组元素     下标        arr
				item:每次循环所获取的数组元素
				index:每次循环的数组元素所对应的数组下标
				arr:当前所循环的数组
			this:
				当前回调函数中this的指向

		◊ myForEach():
			} 声明位置
			} 判断参数个数以及类型
			} 如何循环执行回调函数
			} 回调函数中的this值指向
			var arr = [1, 2, 3, 4];
			        
		        Array.prototype.myForEach = function(){
		            if(arguments.length == 1 && typeof arguments[0] == 'function'){
		                for(var i = 0; i < this.length; i++){
		                    arguments[0].call(window, this[i], i,this);
		                }
		            }else if(arguments.length == 2){
		                // arguments[0]:handler
		                // arguments[1]:this
		                for(var i = 0;i < this.length; i++){
		                    arguments[0].call(arguments[1], this[i], i, this);
		                }
		            }
		        }
		        
		
		        arr.myForEach(function (item, index, arr) {
		            console.log(item, index, arr);
		            // console.log(this);
		        },{});
			
		◊ reduce():
			回调函数 this
			回调函数
				pre item index arr
				pre:prevent,上一次回调函数执行的返回值
					如果第一次执行时,并且没有设置第二个参数时,pre为数组第一个元素;如果设置了第二个参数时,pre为第二个参数
			this:
				回调函数中的this
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值