0

I am sending some encoded data into an array and trying to retrieve them. i am using following code for that

var holiday_list =<?php echo json_encode($calendar_results); ?>;

        var events = []; //The events array

        $.each(holiday_list, function(key, value) {
            events.push({
                title: value.type, //get the type(am or pm)
                start: value.date_cal, //date of the calendar
                // className: "fc-event-skin22"

            });

There are two types. am and pm. i want to get all am results into one variable and all pm results into one variable. If anyone knows how to do that give me an idea. I am using php codeigniter framework. Thanks in advance.

6
  • You will need to show what values value.type contains, is it literally 'am' and 'pm'? Commented Jun 15, 2015 at 8:33
  • 1
    You could use a simple if condition to check wether value.type is am or pm and push that into a seperate array Commented Jun 15, 2015 at 8:33
  • It would be nice if you could post your $calender_results . Commented Jun 15, 2015 at 8:40
  • Post the value of holiday_list Commented Jun 15, 2015 at 8:53
  • @RobG yes. am and pm Commented Jun 15, 2015 at 8:59

3 Answers 3

1

You just need a "if-else". Hope it will work.

        var amEvents = []; //The events array
        var  pmEvents = [];
        $.each(holiday_list, function(key, value) {
           if( value.type == 'am'){
               amEvents.push({
                title: value.type, //get the type(am or pm)
                start: value.date_cal, //date of the calendar
                // className: "fc-event-skin22"

               });

            }
            else{
                pmEvents.push({
                title: value.type, //get the type(am or pm)
                start: value.date_cal, //date of the calendar
                // className: "fc-event-skin22"

               });

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

Comments

0

Hope this will help you

var result;
for( var i = 0, len = holiday_list.length; i < len; i++ ) {
    if( holiday_list[i][title] == 'am' ) {
        result_am[] = holiday_list[i]['start']; 
    }
    else {
        result_pm[] = holiday_list[i]['start']; 
    }        
 }

Comments

0

You can setup the arrays as properties of an object, then assign them based on the type:

var amTypes = [];
var pmTypes = [];
var types = {am:amTypes, pm:pmTypes};

$.each(holiday_list, function(key, value) {
  types[value.type].push({
    title: value.type,
    start: value.date_cal
  });
});    

amTypes and types.am reference the same array, as for pmTypes and types.pm.

Untested since you haven't provided an sample data.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.