After receiving an array of objects from an API, I face the following structure:
Array [
Object {
"OPTIONNUMBER": "1",
"OPTIONVALUE": "Here is your name ",
},
Object {
"OPTIONNUMBER": "2",
"OPTIONVALUE": "Footer",
},
Object {
"OPTIONNUMBER": "3",
"OPTIONVALUE": "https://google.com",
},
]
I am looking for a way to map this data to an object with specific key-value pairs as [OPTIONNAME]: OPTIONVALUE
;
The corresponding model has the shape:
export interface IOptions {
opt1NAME: string;
opt2FOOTER_TEXT: string;
opt3LINK: string;
}
The desired output should resemble:
const options = {
opt1NAME: 'Here is your name';
opt2FOOTER_TEXT: 'Footer';
opt3LINK: 'https://google.com';
}
I have tried using map
and assign
methods but struggled with properly mapping the object names.
edit1:
//appOptions here is an array of string that contains names
let options = {};
for (let i = 0; i < appOptions.length; i++) {
Object.assign(options, {[appOptions[i]]:arr[i].OPTIONVALUE });
}
This approach successfully creates the desired object, but lacks proper typing.