I'm trying to modify a property in an array and add new elements under it.
The array was taken from - https://jsonplaceholder.typicode.com/users
My code just added a new properties instead of modifying them
I tried the following code (by using axios)
const axios = require('axios');
const getData = async() => {
let {data: myData} = await axios.get('https://jsonplaceholder.typicode.com/users')
myData.forEach((e) => {
myData.push({
phone: {
"phoneNumber": e.phone,
"phoneType": ''
}
})
})
console.log(myData)
}
I want to get -
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone":{
"phoneNumber": "1-770-736-8031 x56442",
"phoneType": ''
},
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
But I'm getting
[ { id: 1,
name: 'Leanne Graham',
username: 'Bret',
email: 'Sincere@april.biz',
address:
{ street: 'Kulas Light',
suite: 'Apt. 556',
city: 'Gwenborough',
zipcode: '92998-3874',
geo: [Object] },
phone: '1-770-736-8031 x56442',
website: 'hildegard.org',
company:
{ name: 'Romaguera-Crona',
catchPhrase: 'Multi-layered client-server neural-net',
bs: 'harness real-time e-markets' } },
{ phoneNumber: '1-770-736-8031 x56442', phoneType: '' },
]