简体   繁体   English

Typescript-要替换属性的值,因为它不是默认值

[英]Typescript - To replace the value of a property is it is not the default value

I have 2 objects. 我有2个物件。 The first object A has a default value of 'enabled' for all properties. 对于所有属性,第一个对象A的默认值均为“ enabled”。

public A(): any {
    const enabledObject = { enabled: true };
    return {
        property1: enabledObject,
        property2: enabledObject, 
        property3: enabledObject,
        property4: enabledObject,
    };
}

The second object has different properties 第二个对象具有不同的属性

public B(): any {
    const appProperty = { 
        property1: { enabled: true },
        property2: { enabled: false}, 
        property3: { enabled: true },
        property4: { enabled: false},
    };
}

I want to return object A minus any properties that are specified to be 'enabled=false' in object B. 我想返回对象A减去对象B中指定为“ enabled = false”的所有属性。

public A(): any {
    const enabledObject = { enabled: true };
    return {
        property1: enabledObject, 
        property3: enabledObject,
    };
}

I am attempting to fix this with the following but it does not work. 我正在尝试通过以下方法解决此问题,但它不起作用。

return _.assignInWith(A, B, this.Disabled);

private Disabled(destination: any, source: any, key: any): any {
    const disabledObject = { enabled: false };
    if (_.isUndefined(destination)) {
        return disabledObject;
    }
    return destination;
}

There is also a simple solution without using lodash : 还有一个不使用lodash的简单解决方案:

function AmB(): any {
  const a = A();
  const b = B();
  const res = {};
  for (let aKey in a) {
    if (!b[aKey] || b[aKey].enabled) {
      res[aKey] = a[aKey];
    }
  }
  return res;
}

Stackblitz demo of this example 这个例子的Stackblitz演示

While I do not know loadash syntax, this could be easily done through JavaSscript (or converted to Typescript) 虽然我不知道loadash语法,但可以通过JavaSscript轻松完成(或转换为Typescript)

   var enabledObject = { enabled: true };
    var A = {
      property1: enabledObject,
      property2: enabledObject, 
      property3: enabledObject,
      property4: enabledObject
    };

    var B = {
      property1: { enabled: true },
      property2: { enabled: false}, 
      property3: { enabled: true },
      property4: { enabled: false}
    };


    var diff = {};
    for(var p in A){
        //diff[p] = {};
      //if B does not have the property, add it to result and continue
      if (!B.hasOwnProperty(p)) {
        diff[p] = A[p];

      } else {
        if(B[p].hasOwnProperty('enabled') && !B[p]['enabled']) {
            diff[p] = A[p];
        }
      }
    }

just run this code on the console of your browser and print diff variable 只需在浏览器的控制台上运行此代码并打印diff变量

If you wanted to, you could further generalize the logic to compare two object with different structure and return the differences 如果需要,可以进一步推广逻辑以比较两个具有不同结构的对象并返回差异

You can achieve the following using lodash as follows: 您可以使用lodash如下实现以下目的:

function AmB(): any {
 const a = A();
 const b = B();

 const bPairs = _.toPairs(b);
 const bNotEnabled = _.filter(bPairs, bPair => { return !bPair[1].enabled });
 const aPairs = _.toPairs(a);
 const aMinusB = _.reject(aPairs, aPair => _.find(bNotEnabled, b => b[0] === aPair[0]));
 return {...aMinusB}
}

And you can see this working here 你可以看到这方面的工作在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM