1

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": "[email protected]",
    "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: '[email protected]',
    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: '' },
]
5
  • Why do you have two myData.forEach loops here to begin with? Commented Jul 29, 2019 at 13:27
  • This was part of the code which I forgot to remove. just corrected it, sorry. Commented Jul 29, 2019 at 13:30
  • There is no such thing as a "json array". JSON is a text format. Once you get it from axios, it's just an array. Commented Jul 29, 2019 at 13:31
  • myData is an array, not sure why you expect to get something else after your forEach Commented Jul 29, 2019 at 13:31
  • Possible duplicate of Modify object property in an array of objects Commented Jul 29, 2019 at 13:33

4 Answers 4

1

You can try this:

myData.forEach((e, index) => {
        myData[index].phone =  {
                "phoneNumber": e.phone,
                "phoneType": ''
            }
    })

OR

myData.forEach((e, index) => {
        e.phone =  {
                "phoneNumber": e.phone,
                "phoneType": ''
            }
    })
Sign up to request clarification or add additional context in comments.

Comments

0

myData is an array, so by calling myData.push you are adding one more object to an array.

Using forEach will give you each object in an array in variable e so you need to use the e.

As you want to update object inside array, then you probably want this:

const getData = async() => {
    let {data: myData} = await axios.get('https://jsonplaceholder.typicode.com/users')

    myData.forEach((e) => {
        e.phone = {
                "phoneNumber": e.phone,
                "phoneType": ''
            }
    })
    console.log(myData)
}

Comments

0

You want to transform the existing phone property not push a new object to the array

myData.forEach((e) => {
    e.phone = {
            "phoneNumber": e.phone,
            "phoneType": ''
        };       
});

Comments

0

Use map array function

const data = myData.map(user => ({ ...user, phone: {
    phoneNumber: user.phone,
    phoneType: ''
}}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.