角度2中的angular.equals的等价物

Dme*_*hro 22 object angularjs typescript angular

我正在研究角度1项目到角度2的迁移.在angular 1项目中,我使用angular.equals进行对象比较angular.equals($ctrl.obj1, $ctrl.newObj);,我在线搜索了角度2中的等效方法,但找不到任何匹配的结果.

Dme*_*hro 14

@Günter是的,你是对的,在angular2中没有等价物.在搜索更多的时候,我发现第三方库loadash与angular.equals和syntex的工作方式相同,角度为1,这个库解决了我的问题

代码摘录自loadash文档

var object = { 'a': 1 };
var other = { 'a': 1 };

_.isEqual(object, other);
// => true

object === other;
// => false
Run Code Online (Sandbox Code Playgroud)


Phi*_*hil 12

我重写了Ariels的答案(谢谢!)是TSLINT友好的.如果使用else,你也可以保存一些继续,但我认为这更清楚.也许其他人也需要它:

export function deepEquals(x, y) {
  if (x === y) {
    return true; // if both x and y are null or undefined and exactly the same
  } else if (!(x instanceof Object) || !(y instanceof Object)) {
    return false; // if they are not strictly equal, they both need to be Objects
  } else if (x.constructor !== y.constructor) {
    // they must have the exact same prototype chain, the closest we can do is
    // test their constructor.
    return false;
  } else {
    for (const p in x) {
      if (!x.hasOwnProperty(p)) {
        continue; // other properties were tested using x.constructor === y.constructor
      }
      if (!y.hasOwnProperty(p)) {
        return false; // allows to compare x[ p ] and y[ p ] when set to undefined
      }
      if (x[p] === y[p]) {
        continue; // if they have the same strict value or identity then they are equal
      }
      if (typeof (x[p]) !== 'object') {
        return false; // Numbers, Strings, Functions, Booleans must be strictly equal
      }
      if (!deepEquals(x[p], y[p])) {
        return false;
      }
    }
    for (const p in y) {
      if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) {
        return false;
      }
    }
    return true;
  }
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*ser 6

您可以只使用JSON.stringify并比较两个字符串,而不是编写一个函数来遍历对象?

例:

var obj1 = {
  title: 'title1',
  tags: []
}

var obj2 = {
  title: 'title1',
  tags: ['r']
}


console.log(JSON.stringify(obj1));
console.log(JSON.stringify(obj2));


console.log(JSON.stringify(obj1) === JSON.stringify(obj2));
Run Code Online (Sandbox Code Playgroud)

  • 这适用于大多数情况,但对于语义相同的情况可能会失败.例如,从您的示例http://jsfiddle.net/9d8gjy9e/重新订购成员 (5认同)
  • 因为成员的顺序很重要,这样的比较是不行的 (2认同)

Ari*_*son 5

In Angular 2 you should use pure JavaScript/TypeScript for that so you can add this method to some service

private static equals(x, y) {
    if (x === y)
        return true;
    // if both x and y are null or undefined and exactly the same
    if (!(x instanceof Object) || !(y instanceof Object))
        return false;
    // if they are not strictly equal, they both need to be Objects
    if (x.constructor !== y.constructor)
        return false;
    // they must have the exact same prototype chain, the closest we can do is
    // test there constructor.

    let p;
    for (p in x) {
        if (!x.hasOwnProperty(p))
            continue;
        // other properties were tested using x.constructor === y.constructor
        if (!y.hasOwnProperty(p))
            return false;
        // allows to compare x[ p ] and y[ p ] when set to undefined
        if (x[p] === y[p])
            continue;
        // if they have the same strict value or identity then they are equal
        if (typeof (x[p]) !== "object")
            return false;
        // Numbers, Strings, Functions, Booleans must be strictly equal
        if (!RXBox.equals(x[p], y[p]))
            return false;
    }
    for (p in y) {
        if (y.hasOwnProperty(p) && !x.hasOwnProperty(p))
            return false;
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

  • 什么是RXBox? (2认同)