Lodash - Show changed properties of object

Nic*_*las 3 javascript typescript lodash angular

I have a comparison function that checks if the user has changed any property values in the objects like this.

private checkForChanges(): boolean {
    if (_.isEqual(this.definitionDetails, this.originalDetails) === true) {
        return false;
    } else {
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

this works great, however now I want to console.log changes which property is changed if there are any changes.

I've tried this:

console.log(_.difference(_.keys(this.originalDetails), _.keys(this.definitionDetails)));
Run Code Online (Sandbox Code Playgroud)

but I always get [] so I assume my syntax is wrong.

What's the correct way to achieve what I want?

Mei*_*eir 6

_.isEqual如果值改变可能会返回错误,这可能发生,即使按键保持不变。

例如: {x: 11, y: 12}, and {x: 11, y: 13}尽管它们仍然具有相同的键,但将返回false。

你的 _.difference()比较键。

要获取更改的键,您需要实现自己的功能:

function changedKeys(o1, o2) {
  var keys = _.union(_.keys(o1), _.keys(o2));
  return _.filter(keys, function(key) {
    return o1[key] !== o2[key];
  }
}
Run Code Online (Sandbox Code Playgroud)