Nabeel Ayub
Nabeel Ayub

Reputation: 1250

React object values undefined whereas the object value is present

I have a problem, I have a list of objects like this:

(0)
id: 1
service_name: "ETHYLENE OXIDE STERILIZATION"
service_description: "ETHYLENE OXIDE STERILIZATION Ethylene oxide (EO) is the leading technology. "

(1)

id: 2
service_name: "ETHYLENE STERILIZATION"
service_description: "ETHYLENE OXIDE is good material. "

The above is list of objects.means a list of two objects.Now I want to add a new key in each object i.e image key.I have done through this:

 for (let i=0;i<mydata.length;i++){

        axios.get(api_url)
        .then(res=>{

          for (let it=0;it<res.data.length;it++){
            mylist[it]=res.data[0].service_image
          }

          Object.assign(mydata[i],{"images":mylist}) // making a new key in each object

          mylist=[]

        })

when I print after adding new key:

id: 1
service_name: "ETHYLENE OXIDE STERILIZATION"
service_description: "ETHYLENE OXIDE STERILIZATION Ethylene oxide (EO) is the leading technology. "
images:['url1','url2']

(1)

id: 2
service_name: "ETHYLENE STERILIZATION"
service_description: "ETHYLENE OXIDE is good material"
images:['url3','url4']

I have successfully add new key images to a list of object mydata.but when I access the images attribute by console.log(mydata.images) it gives me undefined however it is present,when I print console.log(mydata) it print whole list and I can see image attribute as well.

i don't what the problem is can someone suggest me

Upvotes: 0

Views: 50

Answers (1)

HermitCrab
HermitCrab

Reputation: 3264

mydata is an array, your images are in:

mydata[0].images
mydata[1].images

Upvotes: 2

Related Questions