0

I'm trying to fill the dropdown with the value of 'startDate' from multiple json objects, how do I do this without also getting 'endDate' and 'price' to show up?

This is my current situation:

Current situation

This is the code I'm using:

function get_months(year) {
    selected_year = year;
    date.style.visibility = 'hidden';
    month.style.visibility = 'visible';

    yearArray = dateType[year];
    console.log(yearArray);

    $.each(yearArray, function(key, value) {
        $(month).append('<option value=' + value + '>' + key + '</option>');
    });
}

function get_dates(month) {
    date_selected = false;

    if(current_type == 'Weekend') {
        date.style.visibility = 'visible';
        //console.log(current_customer, current_type, selected_year, month);
    } else if(current_type == 'Midweek') {
        date.style.visibility = 'visible';
        //console.log(current_customer, current_type, selected_year, month);
    } else if(current_type == 'Week') {
        date.style.visibility = 'visible';
        //console.log(current_customer, current_type, selected_year, month);
    }
    monthArray = yearArray[month];
    console.log(monthArray);
    alert(monthArray);

    $.each(monthArray, function(key, value) {
        //startDate = value.startDate;
        console.log(value.startDate);
        $.each(value, function(key2, value2) {
            /* console.log(key2);
            console.log(value2); */
            $(date).append('<option value=' + key2 + '>' + value2 + '</option>');
        });
    });
}

If any other information is required, please ask! Thanks in advance!

3
  • What exactly are you looking for? Do you want to extract date part from your JSON? Commented Apr 22, 2015 at 12:50
  • @MokshShah As you can see in the image of the current situation it's displaying 'startDate', 'endDate' and 'price' from all 3 objects. I just want to show the values of 'startDate' in the dropdown from all 3 objects. Commented Apr 22, 2015 at 12:52
  • Could you post your JSON ? Commented Apr 22, 2015 at 12:53

2 Answers 2

1

A similar question was answered at this thread which has three different approaches/solutions.

  1. read json, iterate and print values.
  2. hide/show by css
  3. append json kvs.

Hope this helps.

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

Comments

0

You may need to tweak this a bit as I've not properly analyzed what you are doing but it looks like you need to do something like change:

        $(date).append('<option value=' + key2 + '>' + value2 + '</option>');

to:

        if (key2==='startDate') {
            $(date).append('<option value=' + key2 + '>' + value2 + '</option>');
        }

2 Comments

I guess, key2 itself is a value, how this key2==='startDate' will work?
Can't believe I didn't think of a simple if-statement. Thanks! Will accept soon as i can.

Your Answer

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