1. 求最大值
Math.max()是 Math 对象内置的方法,参数是字符串; reduce 是 ES5 的数组 api,参数有函数和默认初始值; 函数有四个参数,pre(上一次的返回值),cur(当前值),curIndex(当前值索引),arr(当前数组)
const arry = [1,2,3,4]
Math.max(...arry ) //4
Math.max.apply(this,arry ) //4
arry.reduce( (prev, cur,curIndex,arr)=> {
return Math.max(prev,cur);
},0) //4
arry.reduce((a,b)=> a>b?a:b) //4
2. 求和
[1,2,3,4].reduce(function (prev, cur) {
return prev + cur;
},0) //10
function sum(arr) {
var len = arr.length;
if(len == 0){
return 0;
} else if (len == 1){
return arr[0];
} else {
return arr[0] + sum(arr.slice(1));
}
}
sum([1,2,3,4]) //10
3. 合并
[1,2,3,4].concat([5,6]) //[1,2,3,4,5,6]
[...[1,2,3,4],...[4,5]] //[1,2,3,4,5,6]
let arrA = [1, 2], arrB = [3, 4]
Array.prototype.push.apply(arrA, arrB))//arrA值为[1,2,3,4]
let arr=[1,2,3,4];
[5,6].map(item=>{
arr.push(item)
})
//arr值为[1,2,3,4,5,6],注意不能直接return出来,return后只会返回[5,6]
4.判断是否包含值
[1,2,3].includes(4) //false
[1,2,3].indexOf(4) //-1 如果存在换回索引
[1, 2, 3].find((item)=>item===3)) //3 如果数组中无值返回undefined
[1, 2, 3].findIndex((item)=>item===3)) //2 如果数组中无值返回-1
[1,2,3].some(item=>{
return item===3
}) //true 如果不包含返回false
5.每一项设置值
[1,2,3].fill(false) //[false,false,false] es6写法
[1,2,3].map(() => 0)
6.每一项是否满足
//every 是 ES5 的 api,每一项满足返回 true
[1,2,3].every(item=>{return item>2}) //false
//some 是 ES5 的 api,有一项满足返回 true
[1,2,3].some(item=>{return item>2}) //true
7.过滤数组
//filter 是 ES5 的 api,返回满足添加的项的数组
[1,2,3].filter(item=>{return item>2}) //[3]
//从数组中过滤虚值 像 0, undefined, null, false, "", ''这样的假值可以通过下面的技巧轻易地过滤掉。
const array = [3, 0, 6, 7, '', false];
array.filter(Boolean);
// 输出 [3, 6, 7]
8.对象和数组转化
Object.keys({name:'张三',age:14}) //['name','age']
Object.values({name:'张三',age:14}) //['张三',14]
Object.entries({name:'张三',age:14}) //[[name,'张三'],[age,14]]
Object.fromEntries([name,'张三'],[age,14]) //ES10的api,Chrome不支持 , firebox输出{name:'张三',age:14}
9.对象数组
[{count:1},{count:2},{count:3}].reduce((p, e)=>p+(e.count), 0)
10.排序
//sort 是 js 内置的排序方法,参数为一个函数
1.数字排序
[1,2,3,4].sort((a, b) => a - b); // [1, 2,3,4],默认是升序
[1,2,3,4].sort((a, b) => b - a); // [4,3,2,1] 降序
//冒泡排序
Array.prototype.bubleSort=function () {
let arr=this,
len = arr.length;
for (let outer = len; outer >= 2; outer--) {
for (let inner = 0; inner <= outer - 1; inner++) {
if (arr[inner] > arr[inner + 1]) {
//升序
[arr[inner], arr[inner + 1]] = [arr[inner + 1], arr[inner]];
console.log([arr[inner], arr[inner + 1]]);
}
}
}
return arr;
}
[1,2,3,4].bubleSort() //[1,2,3,4]
2.字符串排序
const stringArr = ["Joe", "Kapil", "Steve", "Musk"]
stringArr.sort();
// 输出 ["Joe", "Kapil", "Musk", "Steve"]
stringArr.reverse();
// 输出 ["Steve", "Musk", "Kapil", "Joe"]
3.对象数组排序
const objectArr = [
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pig', last_name: 'Bodine' },
{ first_name: 'Pirate', last_name: 'Prentice' }
];
objectArr.sort((a, b) => a.last_name.localeCompare(b.last_name));
// 输出
(3) [{first_name: "Pig", last_name: "Bodine"},
{first_name: "Lazslo", last_name: "Jamf"},
{first_name: "Pirate", last_name: "Prentice"}]
11.去重
//set 是 ES6 新出来的一种一种定义不重复数组的数据类型
//Array.from 是将类数组转化为数组
//...是扩展运算符,将 set 里面的值转化为字符串
Array.from(new Set([1,2,3,3,4,4])) //[1,2,3,4]
[...new Set([1,2,3,3,4,4])] //[1,2,3,4]
const array = [1,2,3,3,4,4];
array.filter((item,idx,arr) => arr.indexOf(item) === idx);
//取新数组存值,循环两个数组值相比较
Array.prototype.distinct = function() {
const map = {}
const result = []
for (const n of this) {
if (!(n in map)) {
map[n] = 1
result.push(n)
}
}
return result
}
[1,2,3,3,4,4].distinct(); //[1,2,3,4]
12.扁平化 n 维数组
//Array.flat(n)是 ES10 扁平数组的 api,n 表示维度,n 值为 Infinity 时维度为无限大
[1,[2,3]].flat(2) //[1,2,3]
[1,[2,3,[4,5]].flat(3) //[1,2,3,4,5]
[1,[2,3,[4,5]]].toString() //'1,2,3,4,5'
//实质是利用递归和数组合并方法 concat 实现扁平
function flatten(arr) {
while(arr.some(item=>Array.isArray(item))) {
arr = [].concat(...arr);
}
return arr;
}
flatten([1,[2,3]]) //[1,2,3]
flatten([1,[2,3,[4,5]]) //[1,2,3,4,5]
13.数字转数组
const numberToArray = number => [...`${number}`].map(el => parseInt(el))
numberToArray(123456) // [1,2,3,4,5,6]