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

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

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?

  1. const bigTodo = {
  2. name: "aaaaaaa",
  3. unwantedProperty: "Nooooooo"
  4. }
  5. export class SmallTodo {
  6. name: string;
  7. constructor(bigTodo: any){
  8. _.assign(this,bigTodo) // NOT working
  9. _.assignIn(this,bigTodo) // NOT working
  10. _.merge(this,bigTodo) // NOT working
  11. _.extend(this,bigTodo) // NOT working
  12. Object.assign(this, fields); // NOT working
  13. for (const prop in bigTodo.keys()) {
  14. if (this.hasOwnProperty(prop)) {
  15. this[prop] = bigTodo[prop]; // NOT working not compatible with type 'any'
  16. }
  17. }
  18. }
  19. }

Expected result:

  1. SmallTodo = {
  2. name: "aaaaaaa"
  3. }

UNWANTED result:

  1. SmallTodo = {
  2. name: "aaaaaaa",
  3. unwantedProperty: "Nooooooo"
  4. }
  5. "
英文:

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?

  1. const bigTodo = {
  2. name: "aaaaaaa",
  3. unwantedProperty: "Nooooooo"
  4. }
  5. .....
  6. export class SmallTodo {
  7. name: string;
  8. constructor(bigTodo: any){
  9. _.assign(this,bigTodo) // NOT working
  10. _.assignIn(this,bigTodo) // NOT working
  11. _.merge(this,bigTodo) // NOT working
  12. _.extend(this,bigTodo) // NOT working
  13. Object.assign(this, fields); // NOT working
  14. for (const prop in bigTodo.keys()) {
  15. if (this.hasOwnProperty(prop)) {
  16. this[prop] = bigTodo[prop]; // NOT working not compatible with type 'any'
  17. }
  18. }
  19. }
  20. }

Expected result:

  1. SmallTodo = {
  2. name: "aaaaaaa"
  3. }

UNWANTED result:

  1. SmallTodo = {
  2. name: "aaaaaaa",
  3. unwantedProperty: "Nooooooo"
  4. }

答案1

得分: 1

以下是翻译好的部分:

  1. 当您使用某个值初始化属性时可以在此属性上调用 getOwnProperty类似于以下方式
  2. const bigTodo = {
  3. name: "aaaaaaa",
  4. unwantedProperty: "Nooooooo"
  5. }
  6. class SmallTodo {
  7. name: string = "";
  8. constructor(bigTodo: any) {
  9. for (const prop in bigTodo) {
  10. if (this.hasOwnProperty(prop)) {
  11. this[prop as keyof typeof this] = bigTodo[prop];
  12. }
  13. }
  14. }
  15. }
  16. console.log(new SmallTodo(bigTodo));
  17. console.log(bigTodo);

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

英文:

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

  1. const bigTodo = {
  2. name: "aaaaaaa",
  3. unwantedProperty: "Nooooooo"
  4. }
  5. class SmallTodo {
  6. name: string = "";
  7. constructor(bigTodo: any) {
  8. for (const prop in bigTodo) {
  9. if (this.hasOwnProperty(prop)) {
  10. this[prop as keyof typeof this] = bigTodo[prop];
  11. }
  12. }
  13. }
  14. }
  15. console.log(new SmallTodo(bigTodo));
  16. 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:

确定