红颜莎娜
/**
*
* @param {Object} sources 传入一个源数据Obj
* @param {Array} rule 传入一个规则数组
* @param {Array} key 传入需要判断的key(sources的属性)
* key要和rule对应
*/function assigment(sources, rule, key){ var target = {} for(let i = 0; i < key.length; i ++){ if(rule[i]){
target[key[i]] = sources[key[i]]
}
} return target
}var sources = {a: 1, b: '', c: '0'}var rule = [
sources.a != '',
sources.b != '',
sources.c != '0',
]var key = ['a', 'b', 'c'];var target = assigment(sources, rule, key)
console.log(target)
当年话下
你这key值都不对应,估计没对接好吧,只能自己映射了,不过为了优雅和减少代码量,可以简单封装一下: /**
* k: req要挂载的key
* p: 源 key
* c: 上下文取值
* v: 不期望的值
**/
function setVal({ k, p, c, v = '' }) {
const _v = c[p] if (_v != v) req[k] = _v
}
var arr = [
{ k: startTs, p: start, c: this.timeSelect },
{ k: endTs, p: end, c: this.timeSelect },
{ k: userId, p: userId, c: this },
{ k: checkType, p: auditType, c: this, v: '1' }
]
arr.forEach(item => setVal(item))