S
S
semki0962019-05-09 20:48:33
Vue.js
semki096, 2019-05-09 20:48:33

How to work with arrays?

When I write alert(data), the alert displays

[{"id":84,"img_name":"o1.jfif","product_id":43,"title":"","alt":"","color_name":"Black"},{"id":85,"img_name":"o2.jfif","product_id":43,"title":"","alt":"","color_name":"OrangeRed"}]

When I write alert(data[0])then the alert displays [
If I write alert(data[0].id)then the alert displays undefined
These manipulations I carry out inside my method in Vue. Can you tell me what is wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Proskurin, 2019-05-09
@semki096

Well, you have a string in data, not an array. This is a JSON string, convert it to an array.
let dataArr = JSON.parse(data);
alert(dataArr[0]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question