1

After I call the API I got this result:

[
    {
        "id_auto_mapping": 1,
        "feed_field": "id",
        "internal_field": "id"
    },
    {
        "id_auto_mapping": 2,
        "feed_field": "url",
        "internal_field": "url"
    },
    {
        "id_auto_mapping": 3,
        "feed_field": "price",
        "internal_field": "price"
    }
]

and I loop through the result and save the keys to autoMappings array:

if( response.data.success ) {
    if( response.data.auto_mappings.length > 0 ) {
        response.data.auto_mappings.forEach( ( item ) => {
            this.autoMappings.push(item.feed_field)
        })
    }
}

Now, If I do console like:

console.log( this.autoMappings )

I got this result:

enter image description here

  1. Why is it saving as an observer instead of normal array?

  2. How can I loop through the observer?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Shibbir
  • 1,963
  • 2
  • 25
  • 48
  • Please ask **one** question per question, not two or more. – T.J. Crowder Jan 24 '23 at 14:13
  • @T.J.Crowder OOPS! I didn't know about it that I can not ask more than 1 questions. – Shibbir Jan 24 '23 at 14:14
  • This question might be related [remove observer from array](https://stackoverflow.com/questions/59610658/how-can-i-remove-ob-observer-from-my-array-list) tl;dr Parse to JSON and back. `const array = JSON.parse(JSON.stringify(this.autoMappings))` – D.Schaller Jan 24 '23 at 14:18
  • Does this answer your question? [how can i remove \_\_ob\_\_: Observer from my array list?](https://stackoverflow.com/questions/59610658/how-can-i-remove-ob-observer-from-my-array-list) – D.Schaller Jan 24 '23 at 14:18
  • @D.Schaller I used that `JSON.parse(JSON.stringify(this.autoMappings))` it's showing me empty array like `[]` – Shibbir Jan 24 '23 at 14:18
  • this is what I can see without and with `JSON.parse(JSON.stringify(this.autoMappings))`: https://prnt.sc/TFLyPHxRLUJm – Shibbir Jan 24 '23 at 14:23
  • Means that it's changed asynchronously and it's really empty at the time when you access it. – Estus Flask Jan 24 '23 at 15:40

1 Answers1

0

you can convert the observer into an array like this :

var parsedobj = JSON.parse(JSON.stringify(obj))
console.log(parsedobj)
nodisplayname
  • 71
  • 1
  • 5