I want to push into array of objects keys and values dynamically. I thought that was pretty simple, something like:
list.forEach(element => {
element["carAttributes"].map((o) =>
{
this.car.push(
{
o.Name: o.Value
}
);
})
});
but it's not working because { o: any; } is not assignable to type CarInterface
where CarInterface
is:
export interface CarInterface {
"name": string;
}
o
returns me something like
{"Name": "name", "Value": "Mercedes"}
what I have to do is take the value of the key Name
and the value of the key Value
and put everything in one object like:
{"name": "Mercedes"}
I have many values so i have to push everything in this.car
list. Is that possible?
EDIT:
It could be something like that
{
"Cars": [{
"carAttributes": [{
"Name": "name",
"Value": "Mercedes"
}, {
"Name": "color",
"Value": "grey"
}, {
"Name": "model",
"Value": "A220"
}],
"available": true,
},{
"carAttributes": [{
"Name": "name",
"Value": "Mercedes"
}, {
"Name": "color",
"Value": "red"
}, {
"Name": "model",
"Value": "B250E"
}],
"available": false,
}]
}
I need to create an array of objects for each car that has this kind of structure
[
{"name": "Mercedes", "color": "grey", "model": "A220"},
{"name": "Mercedes", "color": "red", "model": "B250E"},
]
That's it.