views:

84

answers:

3

I have a function (that I can't change) that queries data from a database and returns it in a variable that shows as the following format if I display it as text:

var outputdata=
    [
        { itemA: 'M0929',  itemDate: new Date(1950,03-1,25,0,0,0,0),  itemID: 'JDR12' },
        { itemA: 'X0121',  itemDate: new Date(1983,07-1,07,8,0,0,0),  itemID: 'RPN50' },
        { itemA: 'U0229',  itemDate: new Date(1942,09-1,07,8,0,0,0),  itemID: 'CRG98' },
    ];

I need it to be converted into the following format (specific date formatting doesn't matter) for use by another function (that I also can't change).

var inputdata=[
        [
            "M0929",
            "1950-03-25",
            "JDR12"
        ],
        [
            "X0121",
            "1983-07-07",
            "RPN50"
        ],
        [
            "U0229",
            "1942-09-07",
            "CRG98"
        ]
    ];

Could someone offer some assistance... I don't really understand javascript arrays and I'm really after a function to do the conversion.

+4  A: 

You're probably going to have to write it yourself, for example:

function pad (what)
{
    return what < 10 ? '0'+what : String(what);
}
function transformData (data)
{
    var result = [];
    for (var i=0;i<data.length;++i)
    {
        var date = data[i]['itemDate'];
        result.push([
            data[i]['itemA'],
            date.getFullYear()+'-'+pad(date.getMonth())+'-'+pad(date.getDate()),
            data[i]['itemID']
        ]);
    }
    return result;
}

var outputdata=
    [
        { itemA: 'M0929',  itemDate: new Date(1950,03-1,25,0,0,0,0),  itemID: 'JDR12' },
        { itemA: 'X0121',  itemDate: new Date(1983,07-1,07,8,0,0,0),  itemID: 'RPN50' },
        { itemA: 'U0229',  itemDate: new Date(1942,09-1,07,8,0,0,0),  itemID: 'CRG98' },
    ];

var result = transformData(outputdata);

alert(result.join("\n"));

Now, the things to be aware of are the nature of UTC dates. More details can be found here http://www.w3schools.com/jsref/jsref_obj_date.asp. Also, I highly recommend reading more about Javascript in general.

Jotham
It's better to hold off on homework questions, rather than just doing it for them.
MarkusQ
If one person (not necessarily this guy) learns something useful on the internet today, it will be a good day.
Jotham
+1  A: 
function convert(outputdata){
  var arr = [];
 for(var i = 0; i<outputdata.length; i++){
    var output = outputdata[i];
    var temp = [output.itemA, output.itemDate, output.itemID];
    arr[i] = temp;
 } 
 return arr;
}

Edited: initialized arr.

Adeel Ansari
It's better to hold off on homework questions, rather than just doing it for them.
MarkusQ
Sound. Will take care in future. For this post can't do much. Even if I delete it, there are revisions available.
Adeel Ansari
There's a difference between a beginner question and homework. I think the OP is just not versed in javascript and/or programming, but it sounds like a real-life problem.
Paolo Bergantino
A: 

Not a full response, because this smells like homework (and if it is, you should tag it as such). So hints first:

  • You can make an array by writing something like `[ 7, 9*7, "ho" ]
  • You can get at properties with dot notation like obj.itemA
MarkusQ
Middle-man = homework? Quite the assumption. And the "warn the masses" repeated comment is excessive, man.
Jonathan Lonowski
It sounded like homework because 1) it's simple enough that someone who can program in _any_ language should be able to suss it out in less time than it takes to post here, 2) with arbitrary "close in" bounds he's not allowed to change 3) posted by a brand new user and 4) ...you get the idea
MarkusQ