简体   繁体   English

合并对象时如何在 object 创建中使用接口

[英]How can I use an interface in an object creation when merging objects

I have a issue im trying to create a new object with a new ID in an existing object but retain the model for the new object我有一个问题,我试图在现有 object 中创建具有新 ID 的新 object 但保留新 ZA8CFDE6331BD59EB661 的 model

Ive tried all methods of trying to get a decent simple way to do this and the way ive achieved it seems awfully wasteful我已经尝试了所有方法来尝试获得一种体面的简单方法来做到这一点,而我实现它的方式似乎非常浪费

  addWindow() {
    let id = 0;

    for (const key in this.windowList) {
      if (parseInt(key, 10) >= id) {
        id = parseInt(key, 10);
      }
    }
    id++;

    const windowItem: WindowModel = {
      id,
      titleBar: 'Test Window ' + id
    };
    const item = {};
    item[id] = windowItem;
    this.windowList = {...this.windowList, ...item};

  }

and my interface is, as for now我的界面是,就目前而言

export interface WindowModel {
    id: number;
    titleBar?: string;
}

What I want is to create a new item with the interface, that is then merged with the old object but retails the model scope.我想要的是使用接口创建一个新项目,然后与旧的 object 合并,但零售 model scope。

You make something with javascript.你用 javascript 做点什么。 Python is not needed fort this.为此不需要 Python。

But here is the answer:但这里是答案:

// finding for 2 // 寻找 2

  numbers = [1,2,3,0,4,5]
sum = 0
for number in numbers:
  if number = 0:
    print("non zero is" + number)
    sum = sum + numbers
print sum

Hope this helps!希望这可以帮助!

Output: 3 Output:3

The output is 3 which is correct output 是 3 这是正确的

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

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