Node.js 如何在TypeScript/NodeJs中的模型中动态创建对象?
我有一个模型,它有一个静态属性,我希望根据输入生成其他属性。我正在使用它来展平JSON。在这里,我可以看到我的对象正在变平,但我没有得到我想要的输出 我试过Node.js 如何在TypeScript/NodeJs中的模型中动态创建对象?,node.js,typescript,flatten,Node.js,Typescript,Flatten,我有一个模型,它有一个静态属性,我希望根据输入生成其他属性。我正在使用它来展平JSON。在这里,我可以看到我的对象正在变平,但我没有得到我想要的输出 我试过[key:string]:any,Record和[]但是看起来我在这里遗漏了什么 注意:正如在StoreStatusChangedModel中提到的,我可以从传入的JSON中获得1..N个对象,但我无法控制它 代码: var flatten = require("flat").flatten; export interf
[key:string]:any
,Record
和[]
但是看起来我在这里遗漏了什么
注意:正如在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",
"online": true,
"message": "The integration adapter is ready."
},
{
"name": "Terminal",
"lastUpdatedTime": "2020-08-13T20:13:12.3163857+00:00",
"online": true,
"message": "Iber service communication OK."
},
{
"name": "Messaging",
"lastUpdatedTime": "2020-08-13T20:13:12.3163858+00:00",
"online": true,
"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."
}
输出:
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",
"online": true,
"message": "The integration adapter is ready."
},
{
"name": "Terminal",
"lastUpdatedTime": "2020-08-13T20:13:12.3163857+00:00",
"online": true,
"message": "Iber service communication OK."
},
{
"name": "Messaging",
"lastUpdatedTime": "2020-08-13T20:13:12.3163858+00:00",
"online": true,
"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."
}
预期输出:
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",
"online": true,
"message": "The integration adapter is ready."
},
{
"name": "Terminal",
"lastUpdatedTime": "2020-08-13T20:13:12.3163857+00:00",
"online": true,
"message": "Iber service communication OK."
},
{
"name": "Messaging",
"lastUpdatedTime": "2020-08-13T20:13:12.3163858+00:00",
"online": true,
"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."
}