比较两个对象的值是否相等

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jbguo/article/details/84633263

方法一: 遍历对象属性值

hasChange: false,
compareForm(nowVal) {
 var oldVal = this.copyForm
  for (const i in nowVal) {
    if (nowVal[i] !== oldVal[i]) {
      this.hasChange = true
      break
    } else {
      this.hasChange = false
    }
  }
  console.log(this.hasChange)
},

方法二:lodash的isEqual,官网示例

var object = { 'a': 1 };
var other = { 'a': 1 };
 
_.isEqual(object, other);
// => true
 
object === other;
// => false

方法三: JSON.stringify()

var object = { 'a': 1 };
var other = { 'a': 2 };
 
JSON.stringify(object) === JSON.stringify(other);
// => true

猜你喜欢

转载自blog.csdn.net/jbguo/article/details/84633263