1 对象深浅拷贝
1 Object.assign(target,source)
2 es6对象扩展运算符
const cloneObj = JSON.parse(JSON.stringify(obj))
function deepClone(obj){
if(!obj || typeof obj!=="object")return;
let newObj=Array.isArray(obj)?[]:{}
for(let key in obj){
if(obj.hasOwnProperty(key)){
newObj[key]=typeof obj[key]==="object"?deepClone(obj[key]):obj[key]
}
}
return newObj;
}
2 防抖
const debounce=(fn,wait,immediate)=>{
let timer=null;
return function (...agrs){
if(timer) clearTimeout(timer);
if(immediate&&!timer){
fn.call(this,args);
}
timer=setTimeout(()=>{
fn.call(this,agrs)
},wait)
};
};
const betterFn=debounce(()=>console.log('fn 防抖执行了'),1000,true)
document.addEventListener("scroll",betterFn)
3 节流
function throttle(fn,wait){
let pre = 0;
return function(...args){
let now = Date.now();
if( now - pre >= wait){
fn.apply(this,args);
pre = now;
}
}
}
function handle(){
console.log(Math.random());
}
window.addEventListener("mousemove",throttle(handle,1000));
4 promise
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class MyPromise {
constructor(executor){
executor(this.resolve, this.reject)
}
status = PENDING;
value = null;
reason = null;
resolve = (value) => {
if (this.status === PENDING) {
this.status = FULFILLED;
this.value = value;
}
}
reject = (reason) => {
if (this.status === PENDING) {
this.status = REJECTED;
this.reason = reason;
}
}
then(onFulfilled, onRejected) {
if (this.status === FULFILLED) {
onFulfilled(this.value);
} else if (this.status === REJECTED) {
onRejected(this.reason);
}
}
}
module.exports = MyPromise
手写promise
class MyPromise {
constructor(executor) {
const resolve = res => {
console.log(res)
}
const reject = rej => {
console.log(rej)
}
executor && executor(resolve, reject)
}
}
const promise = new Promice((resolve, reject) => {
resolve('aa')
reject('err')
})
promise.then(value => {
console.log('resolve', value)
}, reason => {
console.log('reject', reason)
})
5 手写reduce
//不考虑第二个参数:
Array.prototype.reduce = function (cb) {
const arr = this
let total = arr[0]
for (let i = 1
total = cb(total, arr[i], i, arr)
}
return total
}
//考虑第二个参数:
Array.prototype.reduce = function (cb, initialValue) {
const arr = this
let total = initialValue || arr[0]
// 有初始值的话从0遍历,否则从1遍历
for (let i = initialValue ? 0 : 1
total = cb(total, arr[i], i, arr)
}
return total
}
6 数组扁平化
function flatten(arr){
return arr.reduce((result,item)=>{
return result.concat(Array.isArray(item)?flatten(item):item);
},[])
}
function flat(arr, depth = 1) {
if (depth > 0) {
return arr.reduce((pre, cur) => {
return pre.concat(Array.isArray(cur) ? flat(cur, depth - 1) : cur);
}, []);
}
return arr.slice();
}
7 数组去重,数组对象去重
数组
const newArr = [...new Set(arr)]
const newArr = Array.from(new Set(arr))
const arr = [2,7,5,7,2,8,9];
console.log([...new Set(arr)]);
function unique(arr) {
return arr.filter((item, index, array) => {
return array.indexOf(item) === index;
});
}
对象
const list = [{age:18,name:'张三'},{age:18,name:'李四'},{age:18,name:'王五'}]
let hash = {}
const newArr = list.reduce((item, next) => {
hash[next.age] ? '' : hash[next.age] = true && item.push(next)
return item
}, [])
console.log(newArr)
8 instanceof
function myInstanceof(target, origin) {
if (typeof target !== "object" || target === null) return false;
if (typeof origin !== "function")
throw new TypeError("origin must be function");
let proto = Object.getPrototypeOf(target);
while (proto) {
if (proto === origin.prototype) return true;
proto = Object.getPrototypeOf(proto);
}
return false;
}
9 数组排序
sort 排序
// 对数字进行排序,简写
const arr = [3, 2, 4, 1, 5]
arr.sort((a, b) => a - b)
console.log(arr) // [1, 2, 3, 4, 5]
// 对字母进行排序,简写
const arr = ['b', 'c', 'a', 'e', 'd']
arr.sort()
console.log(arr) // ['a', 'b', 'c', 'd', 'e']
冒泡排序
function bubbleSort(arr) {
let len = arr.length
for (let i = 0
// 从第一个元素开始,比较相邻的两个元素,前者大就交换位置
for (let j = 0
if (arr[j] > arr[j + 1]) {
let num = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = num
}
}
// 每次遍历结束,都能找到一个最大值,放在数组最后
}
return arr
}
//测试
console.log(bubbleSort([2, 3, 1, 5, 4])) // [1, 2, 3, 4, 5]
快速排序
//快排
function sortArray(nums) {
quickSort(0, nums.length - 1, nums);
return nums;
}
function quickSort(start, end, arr) {
if (start < end) {
const mid = sort(start, end, arr);
quickSort(start, mid - 1, arr);
quickSort(mid + 1, end, arr);
}
}
function sort(start, end, arr) {
const base = arr[start];
let left = start;
let right = end;
while (left !== right) {
while (arr[right] >= base && right > left) {
right
}
arr[left] = arr[right];
while (arr[left] <= base && right > left) {
left++;
}
arr[right] = arr[left];
}
arr[left] = base;
return left;
}
10 继承
ES5 继承(寄生组合继承)
function Parent(name) {
this.name = name
}
Parent.prototype.eat = function () {
console.log(this.name + ' is eating')
}
function Child(name, age) {
Parent.call(this, name)
this.age = age
}
Child.prototype = Object.create(Parent.prototype)
Child.prototype.constructor = Child
let xm = new Child('xiaoming', 12)
console.log(xm.name)
console.log(xm.age)
xm.eat()
ES6 继承
class Parent {
constructor(name) {
this.name = name
}
eat() {
console.log(this.name + ' is eating')
}
}
class Child extends Parent {
constructor(name, age) {
super(name)
this.age = age
}
}
let xm = new Child('xiaoming', 12)
console.log(xm.name)
console.log(xm.age)
xm.eat()
实现1个 EventBus 类,至少拥有on,emit两个方法
class EventBus {
constructor() {
this.msgList = {}
}
on(msgName, fn) {
if (this.msgList.hasOwnProperty(msgName)) {
if (typeof this.msgList[msgName] === 'function') {
this.msgList[msgName] = [this.msgList[msgName], fn]
} else {
this.msgList[msgName] = [...this.msgList[msgName], fn]
}
} else {
this.msgList[msgName] = fn
}
}
emit(msgName, msg) {
if (!this.msgList.hasOwnProperty(msgName)) {
return
}
if (typeof this.msgList[msgName] === 'function') {
this.msgList[msgName](msg)
} else {
this.msgList[msgName].map((fn) => {
fn(msg)
})
}
}
off(msgName) {
if (!this.msgList.hasOwnProperty(msgName)) {
return
}
delete this.msgList[msgName];
}
}
const bus = new EventBus();
bus.on('event01', (eventArg) => console.log('event01', eventArg));
bus.emit('event01', 123);
给你两个版本号 version1 和 version2比较
function compare( version1 , version2 ) {
let arr1=version1.split(".")
let arr2=version2.split(".")
let length=Math.max(arr1.length,arr2.length)
for (let i = 0
const n1 = Number(arr1[i]||0)
const n2 = Number(arr2[i]||0)
if (n1 > n2) return 1
if (n1 < n2) return -1
}
return 0
}
js 数组对象根据对象中的指定属性去重
(1)方法一:循环判断
function unique(arr,u_key){
let result = []
result[0] = arr[0]
arr.forEach((meta_item,i)=>{
let num = 0
result.forEach((r_item,j)=>{
if (meta_item[u_key]!==r_item[u_key]) {
num++
}
if (num === result.length) {
result.push(meta_item)
}
})
})
return result
}
(2)方法二:利用对象属性唯一
function unique(arr, u_key) {
const obj = {}
const result = []
arr.forEach(item => {
const typeof_key = typeof item[u_key] + item[u_key]
console.log(typeof_key)
obj[typeof_key] = item
})
for (const key in obj) {
result.push(obj[key])
}
return result
}
(3)方法三:利用ES6的Map方法
function unique(arr,u_key) {
let map = new Map()
arr.forEach((item,index)=>{
if (!map.has(item[u_key])){
map.set(item[u_key],item)
}
})
return [...map.values()]
}
(4)方法四:利用lodash库进行处理
如果项目中有引入lodash库的话,,就可以只用使用下面的uniqBy,传入对应的数组对象,和要根据的相关字段即可。
let unarrlist = this.$lodash.uniqBy(Arrlist,'insurerCode')
console.log(unarrlist)
let unArr = this.$lodash.uniqBy(arrayList,'id')
console.log(unArr)