如何在TypeScript / NodeJs中的模型中动态创建对象?

问题描述

我有一个具有一个静态属性的模型,并且我希望基于输入生成其他属性。我正在使用npm flat来展平JSON。在这里,我可以看到我的对象正在展平,但是没有得到想要的输出

我尝试过[key: string]: anyRecord<string,unkNown>[],但似乎我在这里遗漏了一些东西。

注意:在这里,如StoreStatusChangedModel中所述,我可以从传入的JSON中获得1..N个对象,而我对此没有控制权。

代码

var flatten = require("flat").flatten;

export interface StoreStatusChangedModel {
    staticValue: String,[key: string]: any
}

var storeStatusModel = {
    "statusMessages": [
        "The service host is online and ready.","The integration adapter is ready.","Iber service communication OK.","MCAUS-1719 is Connected."
    ]
}

var componentStatesModel = {
    "componentStates": [
        {
            "name": "Host","lastUpdatedTime": "2020-08-13T20:13:12.316377+00:00","online": true,"message": "The service host is online and ready."
        },{
            "name": "Integration","lastUpdatedTime": "2020-08-13T20:13:12.3163855+00:00","message": "The integration adapter is ready."
        },{
            "name": "Terminal","lastUpdatedTime": "2020-08-13T20:13:12.3163857+00:00","message": "Iber service communication OK."
        },{
            "name": "Messaging","lastUpdatedTime": "2020-08-13T20:13:12.3163858+00:00","message": "MCAUS-1719 is Connected."
        }
    ]
}

var obj: StoreStatusChangedModel = {
    staticValue: "staticValue",};
obj.statusMessages = flatten(storeStatusModel.statusMessages);
obj.componentMessages = flatten(componentStatesModel.componentStates);

console.log(obj);

输出

{ 
  staticValue: 'staticValue',statusMessages: 
    { '0': 'The service host is online and ready.','1': 'The integration adapter is ready.','2': 'Iber service communication OK.','3': 'MCAUS-1719 is Connected.'
    },componentMessages: 
   { 
     '0.name': 'Host','0.lastUpdatedTime': '2020-08-13T20: 13: 12.316377+00: 00','0.online': true,'0.message': 'The service host is online and ready.','1.name': 'Integration','1.lastUpdatedTime': '2020-08-13T20: 13: 12.3163855+00: 00','1.online': true,'1.message': 'The integration adapter is ready.','2.name': 'Terminal','2.lastUpdatedTime': '2020-08-13T20: 13: 12.3163857+00: 00','2.online': true,'2.message': 'Iber service communication OK.','3.name': 'Messaging','3.lastUpdatedTime': '2020-08-13T20: 13: 12.3163858+00: 00','3.online': true,'3.message': 'MCAUS-1719 is Connected.'
    }
}

预期输出

{
  "staticValue": "staticValue"
  "statusMessages.0": "The service host is online and ready.","statusMessages.1": "The integration adapter is ready.","statusMessages.2": "Iber service communication OK.","statusMessages.3": "MCAUS-1719 is Connected.","componentStates.0.name": "Host","componentStates.0.lastUpdatedTime": "2020-08-13T20:13:12.316377+00:00","componentStates.0.online": true,"componentStates.0.message": "The service host is online and ready.","componentStates.1.name": "Integration","componentStates.1.lastUpdatedTime": "2020-08-13T20:13:12.3163855+00:00","componentStates.1.online": true,"componentStates.1.message": "The integration adapter is ready.","componentStates.2.name": "Terminal","componentStates.2.lastUpdatedTime": "2020-08-13T20:13:12.3163857+00:00","componentStates.2.online": true,"componentStates.2.message": "Iber service communication OK.","componentStates.3.name": "Messaging","componentStates.3.lastUpdatedTime": "2020-08-13T20:13:12.3163858+00:00","componentStates.3.online": true,"componentStates.3.message": "MCAUS-1719 is Connected."
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)