区分两个对象并仅获取更新

时间:2016-10-03 19:10:37

标签: javascript diff lodash

我试图区分两个对象,只获得更新。

let alpha = {
 brown: true,
 green: true,
 blue: true
}

let beta = {
 green: false,
}

doSomething(alpha, beta) // => {green: false}

let alpha = {
 brown: true,
 green: true,
 blue: true
}

let beta = {
 green: true,
}

doSomething(alpha, beta) // => false

到目前为止,这就是我所拥有的。

let alphaSameKeys = pick(alpha, Object.keys(beta))
console.log(alpha) // => {green: true}

如何使用lodash完成此操作?

1 个答案:

答案 0 :(得分:0)

简单的浅层差异

export function diffObject (source, incoming) {
  let res = pickBy(incoming, (value, key) => {
    if (value !== source[key]) return true
    if (!source[key]) return true
    return false
  })
  if (!size(res)) return false
  return res
}