1

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!

1 Answer 1

4

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

1 Comment

Thanks! That worked. I think I was trying to do something similar from what I found online but I was keeping it inside a function which wasn't working.

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.