I have an array of the following type.
interface IArray {
id: "message1" | "message2" | "message3";
message: string;
}
const myArray: IArray[] = [
{
id: "message1",
message: "Here is your message 1"
},
{
id: "message2",
message: "Here is your message 2"
}
]
I would like to convert it to an indexed object with types as following.
const myObject = {
message1: "Here is your message 1",
message2: "Here is your message 2"
}
I could write a function that converts the array into an object.
const myObject: any = {};
myArray.forEach((arrElement) => {
myObject[arrElement.id] = arrElement.message;
});
But the myObject is of type any. while I want the myObject to have type with indexed keys depending on array id type.
indexed object