防抖
function debounce(func, delay) {
let timerId;
return function (...args) {
clearTimeout(timerId);
timerId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}
function saveData() {
console.log('保存数据');
}
const debouncedSaveData = debounce(saveData, 1000);
debouncedSaveData();
节流
function throttle(func, delay) {
let previousTime = 0;
let timer;
return function throttledFunc() {
const currentTime = Date.now();
if (currentTime - previousTime >= delay) {
func.apply(this, arguments);
previousTime = currentTime;
} else {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, arguments);
previousTime = Date.now();
}, delay);
}
};
}
function doSomething() {
console.log('Doing something...');
}
const throttledFunc = throttle(doSomething, 1000);
throttledFunc();
throttledFunc();
setTimeout(throttledFunc, 2000);
深拷贝
function deepCopy(obj) {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
let copy;
if (Array.isArray(obj)) {
copy = [];
for (let i = 0; i < obj.length; i++) {
copy[i] = deepCopy(obj[i]);
}
} else {
copy = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
copy[key] = deepCopy(obj[key]);
}
}
}
return copy;
}
const deepCopy = deepCopy(originalObject);
一维数组对象的常用操作
根据某个key值是判断一维数组是否有重复项
function hasRepeatOfArr1(arr, key) {
const result = []
arr.forEach((e) => {
result.push(e[key])
})
return new Set(result).size === 1
}
function hasRepeatOfArr2(arr, key) {
return arr.some((item, index) => {
return arr.findIndex((e, i) => e[key] === item[key] && i !== index) !== -1;
});
}
根据某些key值对数据进行过滤并且可以对重复的数据进行操作
function filterAndDoSomething(Array, property) {
const result = Array.reduce((acc, obj) => {
let key = '';
for (const prop in property) {
key += obj[property[prop]] + '-';
}
key = key.slice(0, -1);
if (acc[key]) {
acc[key].age += obj.age;
} else {
acc[key] = { ...obj };
}
return acc;
}, {});
return Object.values(result);
}
const arr = [
{ id: 1, name: 'Alice', age: 20 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 1, name: 'Alice', age: 25 },
{ id: 4, name: 'Charlie', age: 40 },
{ id: 1, name: 'Alice', age: 35 },
{ id: 1, name: 'Bob', age: 45 }
];
console.log(filterAndDoSomething(arr, { id: 'id', name: 'name' }));
[
{ id: 1, name: 'Alice', age: 80 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 4, name: 'Charlie', age: 40 },
{ id: 1, name: 'Bob', age: 45 }
]
筛选出两个数组对象中不同的元素(一维数组)
function findDifferentElements(arr1, arr2, key) {
const set1 = new Set(arr1.map(item => item[key]));
const set2 = new Set(arr2.map(item => item[key]));
const diffElements = [];
for (const item of arr1) {
if (!set2.has(item[key])) {
diffElements.push(item);
}
}
for (const item of arr2) {
if (!set1.has(item[key])) {
diffElements.push(item);
}
}
return diffElements;
}
const arr1 = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Tom' }];
const arr2 = [{ id: 2, name: 'Jane' }, { id: 3, name: 'Tom' }, { id: 4, name: 'Alice' }];
const differentElements = findDifferentElements(arr1,arr2,'id');
console.log(differentElements);
数组对象树结构转换的常用操作
树结构转为平铺结构
let arrTree = [
{ name: '张三', id: 1, children: [{ name: '张小三', id: 11, children: [{ name: '张小三三', id: 111,children:[] }] }] },
{ name: '李四', id: 2, children: [{ name: '李小四', id: 22, children: [{ name: '李小四四', id: 222,children:[] }] }] },
{ name: '王五', id: 3, children: [{ name: '王小五', id: 33, children: [{ name: '王小五五', id: 333,children:[] }] }] },
{ name: '周六', id: 4, children: [{ name: '周小六', id: 44, children: [] }] },
{ name: '桑七', id: 5, children: [] },
]
function treeToRepeat(data,childKey) {
let result = []
function repeatArrFun(data, result) {
if (Array.isArray(data) && data && data.length > 0) {
for (const item of data) {
result.push(item)
if (Array.isArray(item[childKey]) && item[childKey] && item[childKey].length > 0) {
repeatArrFun(item[childKey], result)
}
}
}
}
repeatArrFun(data, result)
return result
}
console.log(JSON.stringify(treeToRepeat(arrTree,'children')));
[
{"name":"张三","id":1,"children":[{"name":"张小三","id":11,"children":[{"name":"张小三三","id":111,"children":[]}]}]},
{"name":"张小三","id":11,"children":[{"name":"张小三三","id":111,"children":[]}]},
{"name":"张小三三","id":111,"children":[]},
{"name":"李四","id":2,"children":[{"name":"李小四","id":22,"children":[{"name":"李小四四","id":222,"children":[]}]}]},
{"name":"李小四","id":22,"children":[{"name":"李小四四","id":222,"children":[]}]},
{"name":"李小四四","id":222,"children":[]},
{"name":"王五","id":3,"children":[{"name":"王小五","id":33,"children":[{"name":"王小五五","id":333,"children":[]}]}]},
{"name":"王小五","id":33,"children":[{"name":"王小五五","id":333,"children":[]}]},
{"name":"王小五五","id":333,"children":[]},
{"name":"周六","id":4,"children":[{"name":"周小六","id":44,"children":[]}]},
{"name":"周小六","id":44,"children":[]},
{"name":"桑七","id":5,"children":[]}
]
平铺结构转为树结构
let arrRepeat = [
{ name: '根节点', id: 1, parentId: null },
{ name: '1-1节点', id: 2, parentId: 1 },
{ name: '1-2节点', id: 3, parentId: 1 },
{ name: '1-3节点', id: 4, parentId: 1 },
{ name: '1-4节点', id: 5, parentId: 1 },
{ name: '1-1-1节点', id: 6, parentId: 2 },
{ name: '1-1-2节点', id: 7, parentId: 2 },
{ name: '1-1-2-1节点', id: 8, parentId: 6 },
{ name: '根节点2', id: 99, parentId: null },
]
function arrayToTree(arr, onlyKey, parentKey) {
if (Array.isArray(arr) && arr.length > 0) {
let map = {};
let tree = [];
for (let i = 0; i < arr.length; i++) {
map[arr[i][onlyKey]] = { ...arr[i], children: [] };
}
console.log(map);
for (let i = 0; i < arr.length; i++) {
let node = map[arr[i][onlyKey]];
console.log(node);
if (node[parentKey] === null) {
tree.push(node);
} else {
let parent = map[node[parentKey]];
if (parent) {
parent.children.push(node);
}
}
}
return tree;
} else {
return null
}
}
console.log(JSON.stringify(arrayToTree(arrRepeat,'id','parentId')));
[
{"name":"根节点","id":1,"parentId":null,
"children":[{"name":"1-1节点","id":2,"parentId":1,"children":[{"name":"1-1-1节点","id":6,"parentId":2,"children":[{"name":"1-1-2-1节点","id":8,"parentId":6,"children":[]}]}, {"name":"1-1-2节点","id":7,"parentId":2,"children":[]}]},
{"name":"1-2节点","id":3,"parentId":1,"children":[]},
{"name":"1-3节点","id":4,"parentId":1,"children":[]},
{"name":"1-4节点","id":5,"parentId":1,"children":[]}]
},
{"name":"根节点2","id":99,"parentId":null,"children":[]}
]