Alice Xu
Alice Xu

Reputation: 543

How does JSON.parse worked?

console.log(this.item_images)
console.log(JSON.parse("["+this.item_images+"]"))

I want to turn this.item_images into an array and loop over it, why can't I parse it? Below is the result in the console of above code. Note : I console.log(typeof this.item_images) it's a string.

enter image description here

Upvotes: 1

Views: 37

Answers (1)

iplus26
iplus26

Reputation: 2647

this.item_images seems to be an array with only one element: the string '038...6aa'

So I guess you may want to split the string into another array.

var arr = this.item_images[0].split(',');

Then you can loop the array arr.

Update: If this.item_images is the string, use:

var arr = this.item_images.split(',');

instead.

Upvotes: 1

Related Questions