1.根据指定字段将相同值得数据分到一起
function sortArr(arr, str,rule) {
var _arr = [],
_t = [],
_tmp;
arr = arr.sort(function (a, b) {
var s = a[str],
t = b[str];
if(rule){
return s - t ;
}else{
return t - s ;
}
});
console.log("arr",arr)
if (arr.length) {
_tmp = arr[0][str];
}
for (var i in arr) {
if (arr[i][str] === _tmp) {
_t.push(arr[i]);
} else {
_tmp = arr[i][str];
_arr.push(_t);
_t = [arr[i]];
}
}
_arr.push(_t);
return _arr;
}
2.深拷贝方法
const deepCp = (data) => {
if (typeof data !== "object") {
return data;
}
let obj;
if (data instanceof Array) {
obj = [];
} else if (data instanceof Map) {
obj = new Map();
} else if (data instanceof Set) {
obj = new Set();
} else if (data instanceof Object) {
obj = {};
}
if (data instanceof Map) {
for (let [key] of data) {
obj.set(key, deepCp(data.get(key)));
}
} else if (data instanceof Set) {
for (let item of data.values()) {
obj.add(deepCp(item));
}
} else {
for (let key of Object.keys(data)) {
let keyConfig = Object.getOwnPropertyDescriptor(data, key);
if (data.hasOwnProperty(key)) {
if (Object.keys(keyConfig).includes("get")) {
Object.defineProperty(
obj,
key,
Object.getOwnPropertyDescriptor(data, key)
);
} else {
Object.defineProperty(obj, key, {
...Object.getOwnPropertyDescriptor(data, key),
value: deepCp(data[key]),
});
}
}
}
}
Reflect.setPrototypeOf(obj, Reflect.getPrototypeOf(data));
return obj;
};
3.对比两个数据是否一致
const deepCompare = (data1, data2) => {
if (typeof data1 !== "object" && typeof data2 !== "object") {
return Object.is(data1, data2);
}
if (data1 instanceof Set && data2 instanceof Set) {
let dataSet1 = deepCp(data1);
let dataSet2 = deepCp(data2);
if (!Object.is(dataSet1.size, dataSet2.size)) {
return false;
} else {
for (let value1 of dataSet1) {
for (let value2 of dataSet2) {
if (deepCompare(value1, value2)) {
dataSet1.delete(value1);
dataSet2.delete(value2);
}
}
}
if (!Object.is(dataSet1.size, 0) || !Object.is(dataSet2.size, 0)) {
return false;
}
}
} else if (data1 instanceof Map && data2 instanceof Map) {
let dataMap1 = deepCp(data1);
let dataMap2 = deepCp(data2);
if (!Object.is(dataMap1.size, dataMap2.size)) {
return false;
} else {
for (let key of dataMap1.keys()) {
if (dataMap2.has(key)) {
if (deepCompare(dataMap1.get(key), dataMap2.get(key))) {
dataMap1.delete(key);
dataMap2.delete(key);
} else {
return false;
}
} else {
return false;
}
}
if (!Object.is(dataMap1.size, 0) || !Object.is(dataMap2.size, 0)) {
return false;
}
}
} else if (data1 instanceof Array && data2 instanceof Array) {
if (Object.is(data1.length, data2.length)) {
let dataSet1 = new Set();
let dataMap1 = new Map();
let dataSet2 = new Set();
let dataMap2 = new Map();
data1.map((value) => {
if (typeof value != "object") {
dataMap1.set(value, dataMap1.has(value) ? dataMap1.get(value) : 1);
} else {
dataSet1.add(value);
}
});
data2.map((value) => {
if (typeof value != "object") {
dataMap2.set(value, dataMap2.has(value) ? dataMap2.get(value) : 1);
} else {
dataSet2.add(value);
}
});
if (
!deepCompare(dataSet1, dataSet2) ||
!deepCompare(dataMap1, dataMap2)
) {
return false;
}
} else {
return false;
}
} else if (data1 instanceof Object && data2 instanceof Object) {
let dataMap1 = new Map(Object.entries(data1));
let dataMap2 = new Map(Object.entries(data2));
if (!deepCompare(dataMap1, dataMap2)) {
return false;
}
} else {
return false;
}
return true;
};
4.对比数组中是否有相同得值
function containsArray(array1, array2) {
for (let i = 0; i < array2.length; i++) {
if (array1.includes(array2[i])) {
return true;
}
}
return false;
}
var array1 = [4,1, 5];
var array2 = [4,2,5,9];
console.log(containsArray(array1, array2));
# 5.防抖函数
```javascript
export const debounce = function () {
let time = null
function res(fn, wait) {
if(time !== null) {
clearTimeout(time)
}
time = setTimeout(fn, wait)
}
return res
}()
5.节流函数
export const throttle = function () {
let flag = false
function res (fn, wait) {
if(flag) {
return
}else {
flag = true
fn()
setTimeout(() => {
flag = false
},wait)
}
}
return res
}()
6.导出方法
function exportClick(data){
let downA = document.createElement("a");
downA.setAttribute("href", data);
downA.innerHTML = "aaaaaaaaaaaaaaaaaaa";
downA.setAttribute("display", "none");
document.body.appendChild(downA);
downA.click();
downA.remove();
}