select specific options in forEach
Hi, guys!
Could you please help me to resolve one task. I get some JSON response from backend side, it's array, now I trying to use cycle forEach for displaing each iterable on html page. It looks like:
- jQuery.each(data, function (key, value) {
- $('
- + '<div>' + "'+ value.title + '" + '</div>'
- + '<div>' + "'+ value.price + '" + '</div>'
- + '<select class="hours f-left">'
- + '<option value="1">1 hour</option>'
- + '<option value="2">2 hour</option>'
- + '<option value="3">3 hour</option>'
- + '</select>'
- ');
- }
In my case, it works for
value.title, and
value.price, but unfurtunatelly I don't know how to select value for select option... I have in value
value.time field, for example -
1, and how can I to select specific option for each iterable element of array?
I tried to use in each iteration this solution
- $('option[value="' + value.time + '"]').attr('selected','selected');
But in my case, last iterable element with
value.time = 3 will be shown for each previous elements. For example: I have next json from backend side.
-
{
-
id:
21,
-
title:
"apple",
-
price:
24,
-
time:
1,
},
-
{
-
id:
22,
-
title:
"banana",
-
price:
29,
-
time:
2,
},
-
{
-
id:
23,
-
title:
"kiwi",
-
price:
12,
-
time:
3,
},
And all elements will be shown with
time = 3, its incorrect : ( I need to show each element on html page with his correct value. Could you tell me please how can I do it?