js数组常用的方法
程序员文章站
2022-06-28 16:14:18
js数组常用的方法排序1.sort()2.sort(callback)reduce(callback, initVal)1. 累加(累乘)2. 统计值的个数3.数组去重遍历1.forEach()2.map()查找是否存在值1.find()2.some()3.includes(val)查找索引1.findIndex()2.indexOf(val)过滤filter()前面添加(删除)1.添加 unshift(val)2.删除 shift()末尾添加(删除)1.添加1.1 push(val)1.2 concat(...
js数组常用的方法
排序
1.sort()
默认按照字符集编码排序
let arr = [11,32,43,3];
arr.sort();
console.log(arr); // [11, 3, 32, 43]
2.sort(callback)
let arr = [11,32,43,3];
arr.sort((a, b) => {
return a - b; // 升序
return b - a; // 降序
});
console.log(arr);
// 升序 [3, 11, 32, 43]
// 降序 [43, 32, 11, 3]
reduce(callback, initVal)
callback参数
pre 初始值或者上次回调的值
cur 当前正在处理的值
index 当前正在处理的下标 (若提供 init 值,则索引为0,否则索引为1)
arr 原数组
1. 累加(累乘)
let arr = [11, 32, 43];
let sum = arr.reduce((pre, cur, index, arr) => {
console.log(pre);
console.log(cur);
console.log(index);
console.log(arr);
return pre + cur;
}, 1);
console.log(sum);
结果
注:不设置初始值时
数组有值
let arr = [11, 32, 43];
let sum = arr.reduce((pre, cur, index, arr) => {
console.log(pre);
console.log(cur);
console.log(index);
console.log(arr);
return pre + cur;
});
console.log(sum);
结果
数组无值
let arr = [];
let sum = arr.reduce((pre, cur, index, arr) => {
console.log(pre);
console.log(cur);
console.log(index);
console.log(arr);
return pre + cur;
});
console.log(sum);
结果
2. 统计值的个数
let arr = [11,32,43,32];
let obj = arr.reduce((pre,cur,index,arr1) => {
if(cur in pre) {
pre[cur] ++;
} else {
pre[cur] = 1;
}
return pre;
}, {})
console.log(obj); // {11: 1, 32: 2, 43: 1}
3.数组去重
let arr = [11, 32, 43, 32];
let obj = arr.reduce((pre, cur, index, arr) => {
if (!pre.includes(cur)) {
return pre.concat(cur);
} else {
return pre;
}
}, []);
console.log(obj); // [11, 32, 43]
4.多维数组转一维数组
let arr = [[3, [5, 4]], [6, 8], [1, 0]];
let arr1 = function f(arr) {
return arr.reduce((pre, cur) => {
return pre.concat(Array.isArray(cur) ? f(cur) : cur);
}, [])
}
console.log(arr1); // [3, 5, 4, 6, 8, 1, 0]
注:二维数组转一维数组
let arr = [[3, 5, 4], [6, 8], [1, 0]];
let arr1 = arr.reduce((pre,cur,index,arr1) => {
console.log(pre);
console.log(cur);
console.log(index);
return pre.concat(cur); // concat()返回一个新的数组
}, []);
console.log(arr1); // [3, 5, 4, 6, 8, 1, 0]
结果
遍历
1.forEach()
let arr = [11,32,43];
arr.forEach((val, index, arr1) => {
console.log(val);
console.log(index);
console.log(arr1);
})
结果
2.map()
let arr = [11,32,43];
arr.map((val, index, arr1) => {
console.log(val);
console.log(index);
console.log(arr1);
})
和forEach()结果相同
查找是否存在值
1.find()
let arr = [11, 32, 43];
let arr2 = arr.find((val, index, arr1) => {
return val == 43; //43
// return val == 42; // "undefined"
});
console.log(arr2);
2.some()
let arr = [11, 32, 43];
let arr2 = arr.some((val, index, arr1) => {
return val == 43; // true
//return val == 42; // false
});
console.log(arr2);
3.includes(val)
let arr = [11, 32, 43];
let arr2 = arr.includes(33);
let arr2 = arr.includes(32);
console.log(arr2);
// false
// true
查找索引
1.findIndex()
let arr = [11, 32, 43];
let arr2 = arr.findIndex((val, index, arr1) => {
return val == 42; // -1
// return val == 43; //2
});
console.log(arr2);
2.indexOf(val)
let arr = [11, 32, 43];
console.log(arr.indexOf(43)); //2
console.log(arr.indexOf(42)); //-1
过滤
filter()
let arr = [11, 32, 43];
let arr2 = arr.filter((val, index, arr1) => {
return val != 43;
})
console.log(arr2); //[11, 32]
前面添加(删除)
1.添加 unshift(val)
let arr = [11, 32, 43];
let arr2 = arr.unshift(2);
console.log(arr2); // 4(数组长度)
console.log(arr); // [2, 11, 32, 43]
2.删除 shift()
let arr = [11, 32, 43];
let arr2 = arr.shift();
console.log(arr2); // 11(被删的数字)
console.log(arr); // [32, 43]
末尾添加(删除)
1.添加
1.1 push(val)
let arr = [11, 32, 43];
let arr2 = arr.push(2);
console.log(arr2); // 4(数组长度)
console.log(arr); // [2, 11, 32, 43]
1.2 concat(val)
let arr = [11, 32, 43];
let arr2 = arr.concat(2);
console.log(arr2); // [11, 32, 43, 2]
console.log(arr); // [11, 32, 43]
注:val也可以是多个值
let arr = [11, 32, 43];
let arr2 = arr.concat(2, 4);
console.log(arr2); // [11, 32, 43, 2, 4]
console.log(arr); // [11, 32, 43]
2.删除 pop()
let arr = [11, 32, 43];
let arr2 = arr.pop();
console.log(arr2); // 43(被删的数字)
console.log(arr); // [11, 32]
任意位置添加(删除)任意个数
1.删除 splice(index, len)
1.1 splice(index, len)
let arr = [11, 32, 43];
let arr2 = arr.splice(1, 1);
console.log(arr2); // [32]
console.log(arr); // [11, 43]
1.2 splice(index)
let arr = [11, 32, 43];
let arr2 = arr.splice(1); // 第二个参数默认为最后一个数字的下标
console.log(arr2); // [32, 43]
console.log(arr); // [11]
2.添加 splice(index, 0, val)
let arr = [11, 32, 43];
let arr2 = arr.splice(1, 0, 22);
console.log(arr2); // []
console.log(arr); // [11, 22, 32, 43]
注:val也可以是多个值
let arr = [11, 32, 43];
let arr2 = arr.splice(1, 0, 22, 23);
console.log(arr2); // []
console.log(arr); // [11, 22, 23, 32, 43]
本文地址:https://blog.csdn.net/qq_44771740/article/details/111084878
上一篇: Spring AOP初始化及执行过程
下一篇: 做拉条子用什么面粉?为什么拉条子容易断?