0

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.

Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
Atlas91
  • 5,754
  • 17
  • 69
  • 141
  • 1
    Did you mean `{ [o.Name]: o.Value }`? That's not valid syntax. What are the types of the existing props? Give a [mre]. *Do* you know that `o.Name` will be in `keyof CarInterface`? – jonrsharpe May 26 '20 at 16:40
  • Yeah I know that is not a valid syntax. That's why I asked how can I do to solve my problem. I just would to take the value of the first property of the object, use it as key and then take the value of the second property and use it as value. I don't know if you get me now – Atlas91 May 26 '20 at 16:44
  • Then you want e.g. https://stackoverflow.com/q/11508463/3001761, use the syntax I showed above. – jonrsharpe May 26 '20 at 16:45
  • Show us the declaration of this.car? – M A Salman May 26 '20 at 16:48
  • I edited my question with the json i have and what I need – Atlas91 May 26 '20 at 17:00

0 Answers0