Lodash一个一致性、模块化、高性能的 JavaScript 实用工具库。
Vue项目引入
npm i lodash-es
import {
cloneDeep,
mergeWith,
throttle,
debounce,
tail,
set,
get,
transform,
isEqual,
isObject,
pick,
groupBy,
camelCase
} from 'lodash-es'
export {
cloneDeep,
mergeWith,
throttle,
debounce,
set,
get,
transform,
isEqual,
isObject,
pick,
groupBy,
camelCase,
}
// mergeWith(object, sources, customizer) 它接受一个 customizer,调用以产生目标对象和来源对象属性的合并值。如果customizer 返回 undefined,将会由合并处理方法代替。 customizer调用与7个参数:(objValue, srcValue, key, object, source, stack)。 这方法会改变对象 object.
// tail(array) 获取除了array数组第一个元素以外的全部元素。
export const merge = function() {
return mergeWith(arguments[0], ...tail(arguments), (objValue, srcValue) => {
if (Array.isArray(objValue)) {
return srcValue
}
})
}
difference()
export const difference = (object, base) => {
function changes(object, base) {
return transform(object, (result, value, key) => {
if (!isEqual(value, base[key])) {
result[key] =
isObject(value) && isObject(base[key]) && !Array.isArray(value)
? changes(value, base[key])
: value
}
})
}
return changes(object, base)
}
来源:CSDN
作者:Web泓
链接:https://blog.csdn.net/weixin_42752574/article/details/103746141