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 Mar 12 at 4:41

1 Answer 1

up vote 1 down vote accepted

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|improve this answer
    
Thank you very much. Yes you are right I need to learn more in javascript. :-) –  Marianna Mar 12 at 5:38
1  
Everyone started from beginner! You will be the same if you keep on learning. –  yazaki Mar 12 at 5:41
    
Hello Yazaki. I have a question, why when I changed the following var table = {}; and map = {}; to var table = [] and map = [];. I get the undefined error? –  Marianna Mar 12 at 10:17
    
Which line did you get the undefined error? Could you setup plnkr or something? –  yazaki Mar 13 at 3:03
    
Hello Yazaki this is the link of my fiddle jsfiddle.net/ftvskpeu/1/.Try to run it, I console log the table. I get the undefined error when I changed them to []. Thanks –  Marianna Mar 13 at 3:16

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.