JS开发需要知道的那些技巧

Array
1.数组交集
普通数组

const arr1 = [1, 2, 3, 4, 5 , 8 ,9],arr2 = [5, 6, 7, 8, 9];
const intersection = arr1.filter((val) => {
return arr2.indexOf(val) > -1
}) // [5, 8, 9]
数组对象

仅支持Number,String,Boolean数据类型

const arr1 = [
{ name: 'name1', id: 1 },
{ name: 'name2', id: 2 }
];
const arr2 = [
{ name: 'name1', id: 1 },
{ name: 'name2', id: 2 },
{ name: 'name3', id: 3 },
];
const intersection = arr2.filter((val) => {
return arr1.some(n => JSON.stringify(n) === JSON.stringify(val))
}) // [{ name: 'name1', id: 1 },{ name: 'name2', id: 2 }]
2.数组并集
普通数组

const arr1 = [1, 2, 3, 6]
const arr2 = [4, 5, 6]
const result = arr1.concat(arr2.filter(v => !arr1.includes(v)))
// [1, 2, 3, 6, 4, 5]
数组对象

const arr1 = [
{ name: 'name1', id: 1 },
{ name: 'name2', id: 2 }
];
const arr2 = [
{ name: 'name1', id: 1 },
{ name: 'name2', id: 2 },
{ name: 'name3', id: 3 },
];
let arr3 = arr1.concat(arr2)
let result = []
let obj = [];
result = arr3.reduce((prev, cur, index, arr) => {
obj[cur.id] ? '' : obj[cur.id] = true && prev.push(cur);
return prev;
}, []);
// [{ name: 'name1', id: 1 },{ name: 'name2', id: 2 },{ name: 'name3', id: 3 }]
3.数组去重
普通数组

Array.from(new Set(1, 2, 3, 3, 3)) // [1, 2, 3]
[...new Set(1, 2, 3, 3)] // [1, 2, 3]
数组对象

const arr = [
{ name: 'name1', id: 1 },
{ name: 'name2', id: 2 },
{ name: 'name3', id: 3 },
{ name: 'name1', id: 1 }
]
const result = []
arr.forEach(item => {
!result.some(v => JSON.stringify(v) === JSON.stringify(item)) && result.push(itme)
}) // [{ name: 'name1', id: 1 }, { name: 'name2', id: 2 }, { name: 'name3', id: 3 }]
4.数组排序
普通数组

[1, 2, 3, 6, 5, 4].sort((a, b) => a - b) // [1, 2, 3, 4, 5, 6]
数组对象

[{name: 'Tom', age: 32}, {name: 'Bob', age: 22}].sort((a, b) => {
return a.age - b.age
}) // [{name: 'Bob', age: 22}, {name: 'Tom', age: 32}]
5.最大值
普通数组

Math.max(...[1, 2, 3, 4]) // 4
Math.max.apply(this, [1, 2, 3, 4]) // 4
[1, 2, 3, 4].reduce((prev, cur, curIndex, arr) => {
return Math.max(prev, cur)
}, 0)
取数组对象中id的最大值

const arr = [{id: 1, name: 'Tom'}, {id: 2, name: 'Bob'}]
const maxValue1 = arr.sort((a, b) => {
return b.id - a.id
})[0].id // 4
const maxValue2 = Math.max.apply(Math, arr.map(item => return item.id)) // 4
6.数组求和
普通数组

[1, 2, 3, 4].reduce(function(prev, cur) {
return prev + cur;
}, 0) // 10
数组对象

const sum = [{num: 1}, {num: 2}].reduce(function(prev, cur) {
return prev + cur.num
}, 0) // 3
7.数组合并
const arr1 = [...[1, 2, 3],...[4, 5, 6]] // [1, 2, 3, 4, 5, 6]
const arr2 = [...[{ num: 1 }],...[{ num: 2 }]] // [ { num: 1 }, { num: 2 } ]
8.数组是否包含值
普通数组

[1, 2, 3].includes(4) // false
[1, 2, 3].indexOf(4) // -1
[1, 2, 3].find(item => item === 3) // 3
[1, 2, 3].findIndex(item => item === 3) // 2
数组对象

cosnt flag = [{num: 1}, {num: 2}].some(v => JSON.stringify(v) === JSON.stringify({num: 2})) // true
9.数组每一项都满足,数组有一项满足
[1, 2, 3].every(item => { return item > 2 }) // false
[{num: 1}, {num: 2}, {num: 3}].every(item => { return item.num > 2}) // false

[1, 2, 3].some(item => { return item > 2 }) // true
[{num: 1}, {num: 2}, {num: 3}].some(item => { return item.num > 2}) // true
10.对象转数组
将数组的key和value转化成数组

Object.keys({name: '张三', age: 1}) // ['name', 'age']
Object.values({name: '张三', age: 1}) // ['张三', 1]
11.数组的深拷贝
遍历复制

// 普通数组
let arr = ['a', 'b'], arrCopy = []
for (let item in arr) arrCopy[item] = arr[item]
arrCopy[1] = 'c'
arr // ['a', 'b']
arrCopy // ['a', 'c']
// 多维数组
let arr = ['a', ['b', ['c']]]
function arrDeepCopy(source) {
let sourceCopy = []
for (let item in source) {
sourceCopy[item] = typeof source[item] === 'object' ? arrDeepCopy(source[item]) : source[item]
return sourceCopy
}
}
slice()

arrayObject.slice(start, end) // 从现有数组中返回选定区间的元素(返回一个新数组)
arrCopy = arr.slice(0)
arrCopy[1] = 'd'
arr // ['a', 'b']
arrCopy // ['a', 'd']
concat()

arrayObject.concat(array1, array2, ...) // 不会改变现有数组
arrCopy = arr.concat();
arrCopy[1] = 'e'
arr // ['a', 'b']
arrCopy // ['a', 'e']
es6的扩展运算符

let a = [1,2,[3,4]]
let b = [...a]
Object
普通对象

let objDeepCopy = function (source) {
let sourceCopy = {}
for (let item in source) {
sourceCopy[item] = source[item]
}
return sourceCopy
}
复杂结构对象

let obj = { "a": { "a1": ["a11", "a12"], "a2": 1 }, "b": 2 };
let objDeepCopy = function (source) {
let sourceCopy = {};
for (let item in source) {
sourceCopy[item] = typeof source[item] === 'object' ? objDeepCopy(source[item]) : source[item]
return sourceCopy
}
}
let objCopy = objDeepCopy(obj)
objCopy.a.a1[1] = "a13";
obj // { "a": { "a1": ["a11", "a12"], "a2": 1 }, "b": 2 }
objCopy // { "a": { "a1": ["a11", "a13"], "a2": 1 }, "b": 2 }

你可能感兴趣的:(JS开发需要知道的那些技巧)