select specific options in forEach

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:


  1. jQuery.each(data, function (key, value) {
  2.    $('
  3.        + '<div>' + "'+ value.title + '" + '</div>'
  4.        + '<div>' + "'+ value.price + '" + '</div>'
  5.        + '<select class="hours f-left">'
  6.            + '<option value="1">1 hour</option>'
  7.            + '<option value="2">2 hour</option>'
  8.            + '<option value="3">3 hour</option>'
  9.        + '</select>'
  10.     ');
  11. }

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

  1. $('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.


  1. {
    • id21,
    • title"apple",
    • price24,
    • time1,
    },
  2. {
    • id22,
    • title"banana",
    • price29,
    • time2,
    },
  3. {
    • id23,
    • title"kiwi",
    • price12,
    • time3,
    },
 
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?