0

Is there an elegant way to convert the following json data to the desired data format below?

original data

{
   "data": [
        {
            "timestamp": 1385118121279,
            "value": 40
        },
        {
            "timestamp": 1385118301279,
            "value": 7
        }

    ]
}

desired format

[[1385118121279,40],[1385118301279,7]]
1
  • Yes, it's very easy with a simple for statement, or use .map() if you prefer. Commented Jan 7, 2014 at 5:12

3 Answers 3

1

Yes, with this:

var transformed = originalObj.data.map(function(item){
   return [item.timestamp, item.value];
});

However, doesn't work in old browsers.

For all browsers including old ones:

var transformed = [];
for(var i in originalObj.data){
    var item = originalObj.data[i];
    transformed.push([item.timestamp, item.value]);
}

Cheers

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

3 Comments

for support in older browsers, jQuery also provides a map function
That's right. And many frameworks, too. But the asker didn't mention jQuery, also it's not in the question tags. Cheers ;)
agreed. Just adding the comment for those who have to support older browsers (like me)
0

This can be done by iterating over object and we can get the array we want.

var abc ={
   "data": [
        {
            "timestamp": 1385118121279,
            "value": 40
        },
        {
            "timestamp": 1385118301279,
            "value": 7
        }

    ]
};
var dataArray = [];
for (var key in abc.data) {
      if(abc.data.hasOwnProperty(key)){
        dataArray.push(abc.data[key].timestamp);
      }
}
console.log(dataArray);

Comments

0

I'm not sure if this is "elegant", but anyway:

function obj2arr(obj){
    if(obj instanceof Array){
        var ret=[];
        for(var i=0;i<obj.length;i++){
            if(typeof obj[i]=="object"){
                ret.push(obj2arr(obj[i]));
            }else{
                ret.push(obj[i]);
            }
        }
        return ret;
    }else if(typeof obj=="object"){
        var ret=[];
        var keys=Object.getOwnPropertyNames(obj);
        for(var i=0;i<keys.length;i++){
            if(typeof obj[keys[i]]=="object"){
                ret.push(obj2arr(obj[keys[i]]));
            }else{
                ret.push(obj[keys[i]]);
            }
        }
        return ret;
    }else{
        return obj;
    }
}

The following test:

console.log(obj2arr({
    "data": [{
        "timestamp": 1385118121279,
        "value": 40
    },{
        "timestamp": 1385118301279,
        "value": 7
    }]
}));

Outputs:

[ [ [ 1385118121279, 40 ], [ 1385118301279, 7 ] ] ]

Online demo (I'm surprised to see that the JS engine in Ideone doesn't support Object.getOwnPropertyNames)

Although please be noted that this is a recursive solution, so big objects may causes stack overflow...

Comments

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.