From f919511ebd81b7446779bea7e7bbcc1bb981545f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=87=AA=E5=B8=A6=E5=A4=A7=E4=BD=AC=E6=B0=94=E5=9C=BA?= <188633308@qq.com> Date: Thu, 3 Jun 2021 15:02:43 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=89=A9=E5=B1=95lodash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Web/src/main.js | 2 + Web/src/util/lodash-extend/index.js | 59 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 Web/src/util/lodash-extend/index.js diff --git a/Web/src/main.js b/Web/src/main.js index 3e6598c..ab56557 100644 --- a/Web/src/main.js +++ b/Web/src/main.js @@ -39,6 +39,8 @@ Vue.prototype.$api = api * Lodash全局化 */ import _ from 'lodash' +import * as _extend from './util/lodash-extend' +Object.assign(_, _extend) Vue.prototype.$_ = _ /** * moment全局化 diff --git a/Web/src/util/lodash-extend/index.js b/Web/src/util/lodash-extend/index.js new file mode 100644 index 0000000..c707c47 --- /dev/null +++ b/Web/src/util/lodash-extend/index.js @@ -0,0 +1,59 @@ +/** + * 将源对象的属性值解析到目标对象已存在的属性 + * @param {Object} target + * @param {...Object} sources + * @returns target + * give({a:1,b:2},{b:3,c:4}) + * => {a:1,b:3} + */ +export const give = (target, ...sources) => { + target = Object(target) + sources.forEach(source => { + if (source) { + source = Object(source) + for (const key in target) { + if (source.hasOwnProperty(key)) { + const _s = source[key] + if (_s !== undefined && _s !== null) { + target[key] = _s + } + } + } + } + }) + return target +} + +/** + * 同give,深度遍历 + * @param {Object} target + * @param {...Object} sources + * @returns target + * giveDeep({a:1,b:{x:1,y:2}},{b:{x:3,z:4},c:3}) + * => {a:1,b:{x:3,y:2}} + */ +export const giveDeep = (target, ...sources) => { + const _deep = (_target, _source) => { + for (const key in _target) { + if (_source.hasOwnProperty(key)) { + const _t = _target[key], + _s = _source[key] + if (_s !== undefined && _s !== null) { + if (_t && _t.constructor === Object) { + _deep(_t, _s) + } else { + _target[key] = _s + } + } + } + } + } + target = Object(target) + sources.forEach(source => { + if (source) { + source = Object(source) + _deep(target, source) + } + }) + return target +} \ No newline at end of file