I'm very beginner in coding. Please help me with this:
I need to add an attribute 'name' to my array.
I got each element separately but don't know how to add attribute "name" to it :/ is it possible ?
Working with graphql an TS, I got this:
"myTypes": [ "cat", "dog", "bird", "butterfly" ]
and I need to become it like this :
"myTypes": [ {name: "cat"}, {name:"dog"}, {name: "bird"}, {name: "butterfly"}]
My code:
const mydata = response.data.myTypes let names = Array.from(mydata).forEach((element: any) => console.log(element))
thanks for your help !
CodePudding user response:
You can use the map function it takes a callback that returns the new array with the modifications you made inside the map. Also your callback should return the new array item everytime it should be like this
const myNewTypes=myTypes.map((type)=>{return { name: type }})