如何在TypeScript中动态从一个对象复制值到’this’,而不修改目标对象?

huangapple go评论110阅读模式
英文:

How to copy values dynamically from one object to 'this' without modifying the destination in Typescript?

问题

以下是您要翻译的内容:

"After searching everywhere, I didn't find a working answer.

How to map values dynamically from one object to 'this' without modifying the target in Typescript?

const bigTodo = {
  name: "aaaaaaa",
  unwantedProperty: "Nooooooo"
}

export class SmallTodo {
    name: string;

    constructor(bigTodo: any){
        _.assign(this,bigTodo)       // NOT working
        _.assignIn(this,bigTodo)     // NOT working
        _.merge(this,bigTodo)        // NOT working
        _.extend(this,bigTodo)       // NOT working
        Object.assign(this, fields); // NOT working

        for (const prop in bigTodo.keys()) { 
          if (this.hasOwnProperty(prop)) {
            this[prop] = bigTodo[prop]; // NOT working not compatible with type 'any'
          }
        }
    }
}

Expected result:

SmallTodo = {
    name: "aaaaaaa"
}

UNWANTED result:

SmallTodo = {
    name: "aaaaaaa",
    unwantedProperty: "Nooooooo"
}
"
英文:

After searching everywhere, I didn't find a working answer.

How to map values dynamically from one object to 'this' without modifying the target in Typescript?

const bigTodo = {
  name: "aaaaaaa",
  unwantedProperty: "Nooooooo"
}

.....

export class SmallTodo {
    name: string;

    constructor(bigTodo: any){
        _.assign(this,bigTodo)       // NOT working
        _.assignIn(this,bigTodo)     // NOT working
        _.merge(this,bigTodo)        // NOT working
        _.extend(this,bigTodo)       // NOT working
        Object.assign(this, fields); // NOT working

        for (const prop in bigTodo.keys()) { 
          if (this.hasOwnProperty(prop)) {
            this[prop] = bigTodo[prop]; // NOT working not compatible with type 'any'
          }
        }
    }
}

Expected result:

SmallTodo = {
    name: "aaaaaaa"
}

UNWANTED result:

SmallTodo = {
    name: "aaaaaaa",
    unwantedProperty: "Nooooooo"
}

答案1

得分: 1

以下是翻译好的部分:

当您使用某个值初始化属性时可以在此属性上调用 getOwnProperty类似于以下方式

const bigTodo = {
  name: "aaaaaaa",
  unwantedProperty: "Nooooooo"
}

class SmallTodo {
  name: string = "";

  constructor(bigTodo: any) {    
    for (const prop in bigTodo) {
      if (this.hasOwnProperty(prop)) {
        this[prop as keyof typeof this] = bigTodo[prop];
      }
    }
  }
}

console.log(new SmallTodo(bigTodo));
console.log(bigTodo);

如果您需要进一步的翻译或有其他问题,请随时提出。

英文:

When you initialise property with some value you can call getOwnProperty on this. Something like below

const bigTodo = {
  name: "aaaaaaa",
  unwantedProperty: "Nooooooo"
}

class SmallTodo {
  name: string = "";

  constructor(bigTodo: any) {    
    for (const prop in bigTodo) {
      if (this.hasOwnProperty(prop)) {
        this[prop as keyof typeof this] = bigTodo[prop];
      }
    }
  }
}


console.log(new SmallTodo(bigTodo));
console.log(bigTodo);

huangapple
  • 本文由 发表于 2023年3月9日 14:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定