程序媛小琦(数组方法)(下)

一、数组方法concat

concat()方法可以在现有数组全部元素基础上创建一个新数组。

//代码演示
let colors = ["red", "green", "blue"]; 
let colors2 = colors.concat("yellow", ["black", "brown"]); 
console.log(colors2); // ["red", "green", "blue", "yellow", "black", "brown"]
//不能添加第二层的数据
let colors = ["red", "green", "blue"]; 
let colors2 = colors.concat("yellow", ["black", "brown"],"aaa",["bbb",'ccc', ['a','b',['c']]]);
console.log(colors2);	//["red", "green", "blue", "yellow", "black", "brown", "aaa", "bbb", "ccc", Array(3)]

二、数组方法slice

//代码演示
let colors = ["red", "green", "blue", "yellow", "purple"]; 
let colors2 = colors.slice(1); 
console.log(colors2);	// ["green", "blue", "yellow", "purple"
let colors3 = colors.slice(1, 4);
console.log(colors3);	// ["green", "blue", "yellow"]

三、数组方法splice

splice()是非常强大的方法,可删除,替换,增加,会改变原数组。

//删除
let colors = ["red", "green", "blue"]; 
let removed = colors.splice(0,1); // 删除第一项,并将第一项返回
console.log(colors);	// ["green", "blue"]
console.log(removed);	// ["red"]

let colors = ["red", "green", "blue"]; 
let removed = colors.splice(-3);
console.log(removed);	// ["red", "green", "blue"]

let colors = ["red", "green", "blue"]; 
let removed = colors.splice(-3,2);
console.log(removed);	// ["red", "green"]
//插入
let colors = ["red", "green", "blue"]; 
let removed = colors.splice(1, 0, "yellow", "orange"); // 在位置1插入两个元素
console.log(colors);	// ["red", "yellow", "orange", "green", "blue"]
console.log(removed);	// []

//替换
比如,`splice(2, 1, "red", "green")` 会在位置2删除一个元素,然后从该位置开始向数组中插入 "red""green"let colors = ["red", "green", "blue"]; 
let removed = colors.splice(1, 1, "red", "purple"); // 插入两个值,删除一个元素
console.log(colors);	// ["red", "red", "purple", "blue"]

四、寻找下标indexOf/lastIndexOf

indexOf:从左到右找第一个===的下标

[1,2,3].indexOf(1);	// 0
[1,2,3].indexOf('1');	// -1,因为不全等,找不到就返回-1
[1,2,3,1,5,6].indexOf(1, 2);	// 3,第二个参数是从x下标开始搜索

lastIndexOf:从右往左找第一个===的下标

[1,2,3,1,5,7].lastIndexOf(1);// 3
[1,2,3,1,5,7,1,5].lastIndexOf(1, 5);// 3

五、数组方法includes

判断数组内是否有===的项,只要有全等的项就是true

[1,2,3,4,5,6].includes(1);	// true
[1,2,3,4,5,6].includes('1');	// false,因为不全等
[1,2,3,4,5,6].includes(8);	// false

六、数组方法find

根据条件查找数组内的单个项,根据条件查找项,只要返回true那就证明找到了,如果为false的话,那就继续遍历查找

[1,2,3].find(c=> c === 1);	// 1
[1,2,3].find(c=> c == '1');	// 1
[1,2,3].find(c=> c === 5);	// undefined
[1,2,3].find(c=> c > 1);	// 2

七、数组方法findIndex

如果找到就返回下标 找不到就返回-1

[{ age: 20 }, { age: 18 }, { age: 10 }].findIndex(function (item) {
	return item.age === 10
});	//2
[1,2,3].findIndex(c=> c === 5);	// -1
[1,2,3].findIndex(c=> c > 1);	// 1

八、数组方法every

验证数组内每一个项是否匹配

[1,2,3].every(c=> c === 3);	// false
[1,2,3].every(c=> c > 0);	// true
[1,2,3].every(c=> c < 10);	// true
[1,1,1].every(c=> c === 1);	// true

九、数组方法some

验证数组内某一个项是否匹配

[1,2,3].some(c=> c === 3);	// true
[1,2,3].some(c=> c > 0);	// true
[1,2,3].some(c=> c < 10);	// true
[1,1,1].some(c=> c === 1);	// true
[1,2,3].some(c=> c === 5);	// false

十、数组方法filter

只要符合条件的元素就返回,如果没有就返回空

[1,2,3].filter(c=> c > 1);	// [2,3]
[1,2,3].filter(c=> c > 5);	// []
[1,2,3].filter(c=> c === 3);	// [3]

十一、数组方法forEach

遍历每一个数组,传入回调函数,可对每一个项进行操作(用return无法停止,会遍历完所有的项)

[1, 2, 3].forEach(c => {
    console.log(c * 2);
});
// 2 4 6

[1, 2, 3].forEach((item, index, array) => {
    console.log(item * index);
});
// 0 2 6
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值