Search code examples
javascriptarraysjsonflot

Converting JavaScript object/map into nested arrays?


I'm trying to use Flot.js to chart some data. My JSON response contains this type of data mapping:

{123: 5, 534: 0, 724: 3}

I would like to convert that to:

[[123, 5], [534, 0], [724, 3]]

for use with Flot Charts. I also need to convert every element to a number instead of a String.

Is there an existing function to do this? Can't for the life of me find it.

Thanks!


Solution

  • Try this:

    var obj = {123: 5, 534: 0, 724: 3};
    var pairs = Object.keys(obj).map(function (key) {
        return [Number(key), Number(obj[key])];
    });
    
    console.log(pairs);