Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this data inside my controller form:

 $scope.reports = [
            {
                departuredate:"2015-03-10",
                routeline:"PASAY - CAGAYAN",
                seatingtypescode:"ABS",
                tickettripcode:"3",
                tripcodetime:"16:30:00"
            },
            {
                departuredate:"2015-03-10",
                routeline:"PASAY - CAGAYAN",
                seatingtypescode:"ABS",
                tickettripcode:"3",
                tripcodetime:"16:30:00"
            }
          ];

The above data is array of objects, I want them to convert in array. I used this code below to convert them in array:

var details=[];

        for(var index in $scope.reports){
            var tripcode = $scope.reports[index].tickettripcode;
            var dateOfDepature = $scope.reports[index].departuredate.split('-');
            details.push(tripcode, dateOfDepature[2]);
        }
if(details[tripcode][dateOfDeparture[2]]){
    details[tripcode][dateOfDeparture[2]] = details[tripcode][dateOfDeparture[2]] +1;
}
else {
    details[tripcode][dateOfDeparture[2]] =1;
}

The code is not working fine with me and I do not know why. I have a doubt if I am doing array manipulation in a right way. I have error dateOfDeparture is not defined. I already defined the dateOfDeparture so why I getting this error. I just wanted to get the output which looks like this:

    details = Array (
                [3] =>Array
                (
                    [10] =>2
                )
    )

The [3]is tickettripcode and [10] is the day of depaturdate. The 2 means number of departuredate in that date.

Any help would be much appreciated. This is the link of my fiddle : https://jsfiddle.net/n1bw2u36/ Thanks in advance!

share|improve this question
    
It's not clear what you want to do. Update your question with the object that you want to be the result of your function so we can better understand what you want –  ThiagoPXP 9 mins ago

1 Answer 1

Unfortunately there are a lot of things you should learn about javascript.

By the way, I would expect that you will get what you want with the code like this.

var reports = [
    {
        departuredate:"2015-03-10",
        routeline:"PASAY - CAGAYAN",
        seatingtypescode:"ABS",
        tickettripcode:"3",
        tripcodetime:"16:30:00"
    },
    {
        departuredate:"2015-03-10",
        routeline:"PASAY - CAGAYAN",
        seatingtypescode:"ABS",
        tickettripcode:"3",
        tripcodetime:"16:30:00"
    }
];

var table = {};

for (index=0; index<reports.length; index++){

    var tripcode = reports[index].tickettripcode;
    var dateOfDepature = reports[index].departuredate.split('-');
    var date = dateOfDepature[2];

    var map = table[tripcode];
    if (map===undefined){
        map = {};
        table[tripcode] = map;
    }

    if (map[date]===undefined){
        map[date] = 0;
    }

    map[date]++;

}

You can use "table" like this.

console.log(table);
console.log(table[3][10]);
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.