0

I receive from the server a JSON like the following:

{"sent":{"Executive":1},"received":{"Executive":1},"viewed":{"Executive":1}}

how to convert this structure to a JavaScript multidimensional array?

I would want something like this:

var sent = [["Executive", 1]];

var received = [["Executive", 1]];

var viewed = [["Executive", 1]];

2 Answers 2

1

The simplest and most dynamic way surely is a good old for-loop. Javascript doesn't ship with built-ins making this task easier.

An example of a 1d-array. I leave it open to the thread creator to add another, inner for-loop.

var json_data = {"sent": 1, "received": 2};
var result = [];

for(var i in json_data)
    result.push([i, json_data [i]]);
Sign up to request clarification or add additional context in comments.

Comments

0
var plotdata = []; 
            var scale = [];
            var color = new Array();
            color[0] = "#3983C2";
            color[1] = "#F79263";
            color[2] = "#CB6FD7";
            color[3] = "#87B87F";
            color[4] = "#9ABC32";
            color[5] = "#D53F40";
            color[6] = "#999999";

            var x = 0;
            for(var i in json_data) {       
                var obj = json_data[i];
                var d1 = [];            
                for(var k in obj) {
                    d1.push([k, obj [k]]);  
                    scale.push([obj [k]]);      
                }
                x++;
                plotdata.push({ color: color[x], label: i, data: d1});
            }

1 Comment

If you are answering your own question, then please provide a proper explanation at least, don't just post code. At first glance, the code has nothing to do with the code in the question.

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.