0

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!

1
  • 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 Commented Mar 12, 2015 at 4:41

1 Answer 1

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]);
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you very much. Yes you are right I need to learn more in javascript. :-)
Everyone started from beginner! You will be the same if you keep on learning.
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?
Which line did you get the undefined error? Could you setup plnkr or something?
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
|

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.