5

Note please. (I'm currently using the Spring Framework (MVC))

The value sent from Controller to ajax is ...

enter image description here

It looks like the picture above.

I am looping through the forEach statement for the values in that array, and I want to put the value into the tag.

So I wrote the code.

$(function()
    {
        $('#doctorSelect').change(function()
        {
            $('#selectGugan').show();

            var doctor_idx = $(this).val();
            $.ajax
            ({
                type: 'POST',
                url: 'selectDoctor.do',
                data: {"d_idx":doctor_idx},
                dataType: 'JSON',
                success: function(sectionDate)
                {
                    console.log(sectionDate);
                    var html = "<option value=''>Choice Doctor</option>";
                    var responseData = [];
                    responseData.push(sectionDate);
                    console.log(responseData);

                    $.each(responseData, function(key, value)
                    {
                        console.log("forEach statement in responseData :" + responseData);

                        //html+="<option value="+new_date+">"+new_date+"</option>"; 
                    });
                    $('#doctorSelect_2').html(html);
                },
                error: function(sectionDate)
                {
                    console.log("data : " + sectionDate);
                }
            });
        });
    });

But unexpectedly, I do not get the key, value.

In fact, I don t know about the jquery forEach statement.

How do I set key, value?

I just want to bring those values and express it like this.

<option value="ri_idx">ri_startDate ~ ri_endDate</option>

How to set key, value or How to use jquery forEach statement ?

I am a beginner. I would appreciate it if you could give me an example.

8
  • 1
    console.log("forEach statement in responseData :" + responseData); replace this with console.log(key); console.log(value); and you will get individual key/val Commented Dec 15, 2017 at 9:24
  • OK, I will fix it. Commented Dec 15, 2017 at 9:25
  • 0 / [object Object] (key / value) Commented Dec 15, 2017 at 9:26
  • 1
    well now to get the individual properties of value, you can simply do value.propertyName, where propertyName is whatever is in your response. Commented Dec 15, 2017 at 9:27
  • 1
    oh why would you do responseData.push(sectionDate)? You can simply do $.each(sectionDate, function(key, value){ ... }); and then it should work. Hope this helps Commented Dec 15, 2017 at 9:31

3 Answers 3

4

In your case I am not sure why would you be doing this:

responseData.push(sectionData);

because this way you dont get an array of objects as I believe you thought you would, you simply will get an array with 1 element in it, which is many objects, so doing a forEach on that array will not help, because the value will be the multiobject element (not a single object that you could access properties).

What you want to do is iterate over your original objects, so your code should be something like this:

$.each(sectionDate, function(key, value){
    // here you can access all the properties just by typing either value.propertyName or value["propertyName"]
    // example: value.ri_idx; value.ri_startDate; value.ri_endDate;
});

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

2

In jQuery forEach working like this

$.each( obj, function( key, value ) {
  alert( key + ": " + value );
});

If you are using Array for loop than key is array index and value values and if you are using javascript object then object key is key and value is object value for the particular key.

You can read more at jQuery documentation.

1 Comment

The answer was obscured. Now I've checked your answer. Your answer also helped me a lot. Thank you.
0

Just use property names in object. Check if this helps

$(document).ready(function() {
  var responseData = [
    {startdate: '2017-12-21', enddate: '2017-12-22', idx: '1'},
    {startdate: '2017-11-21', enddate: '2017-11-22', idx: '1'},
    {startdate: '2017-10-21', enddate: '2017-10-22', idx: '1'}
  ];
  var html = '';
  $.each(responseData, function(key, value) {
    html += "<option value=" + value.startdate + ">" + value.startdate + "</option>";
  });
  $('#doctorSelect').html(html);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.