1

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.

1
  • Thanks for the responses - I'll give some of them a go. No its not homework... just need to get data out of a system using their database function and into a dhtmlxGrid control. Commented Mar 19, 2009 at 1:58

3 Answers 3

4

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.

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

2 Comments

It's better to hold off on homework questions, rather than just doing it for them.
If one person (not necessarily this guy) learns something useful on the internet today, it will be a good day.
1
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.

3 Comments

It's better to hold off on homework questions, rather than just doing it for them.
Sound. Will take care in future. For this post can't do much. Even if I delete it, there are revisions available.
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.
0

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

2 Comments

Middle-man = homework? Quite the assumption. And the "warn the masses" repeated comment is excessive, man.
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

Your Answer

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