I am trying to define the output of this Typescript function.
function (data: { topicId: number; subTopicId: number; topicName: string; subTopicName; string; }[] ) {
var output = <IAnything>{
dataMap: _.reduce(data, function (rv, v) {
rv[v.subTopicId] = v;
return rv;
}, {});
I could map some parts which I didn't include in this question but I am getting confused on how to make the dataMap field. Can someone help me and tell me how I can map the output of the lodash _.reduce in the interface below. From what I can see the output of this reduce is:
data: { topicId: number; subTopicId: number; topicName: string; subTopicName; string; }[]
but how can I represent this and how can I represent the subTopicId that is used for the index of the array?
interface IAnything {
//data: { id: number; name: string; }[];
dataMap:
}
Here is what the dataMap output looks like:
{
"1":{"topicId":1,
"subTopicId":1,
"topicName":"x",
"subTopicName":"x"},
"2":{"topicId":1,
"subTopicId":2,
"topicName":"x",
"subTopicName":"x"},
"62":{"topicId":10,
"subTopicId":62,
"topicName":"x",
"subTopicName":"x"}
}
Your interfaces should look like this:
interface IAnything
{
dataMap: IMap
}
interface IData
{
topicId: number;
subTopicId: number;
topicName: string;
subTopicName; string;
}
interface IMap{
[key: string] : IData;
}
Then your function would look like this:
function (data: IData[]){
var output = <IAnything>{
dataMap: _.reduce(data, function (rv: IData, v: IData)
{
rv[v.subTopicId] = v;
return rv;
}, {})
};
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments