0

I have this array of Object that I am getting from my database:

[Array of Object][1]

I would like to make an array of array for each value, but I can't manage to find a way to do it as I'm a beginner of javascript.

For example :

var Stats=[
  [39,49,43,42,41,35], //SGW Value for each Object
  [37,44,49,46,52,42], //UD Value for each Object 
  [8,11,8,8,16,15],    //Virtual Value for each Object
  ...     
]

The goal is to make a chart on chart.js that look like that :

[Chart Goal][2]

I would need to loop the dataset because I'll add more data and it would be way too long to set each dataset individually.

Thanks for your time.

2
  • 1
    It's not array of objects.. it's a multi-dimensional array. Commented Aug 7, 2018 at 10:13
  • better is can you show an example of how do you want? Commented Aug 7, 2018 at 10:22

2 Answers 2

1

You can do it like this:

let array1 = [
  {
    param1: 10,
    param2: 20
  },
  {
    param1: 30,
    param2: 40
  }
]

let array2 = array1.map(item => Object.values(item));

console.log(array2); // prints [[10, 20], [30, 40]]

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

1 Comment

That's not the answer to this exact question, but that's what I was looking for! Thanks!
0

First of all you need to create an array for each property you want to plot; i.e.:

var fsp = [],
    msg = [],
    sgw = [];

Then you can loop over your dataset and put the data in each array:

yourArray.forEach(function(obj){
    //obj takes the value of each object in the database
    fsp.push(obj.fsp);
    msg.push(obj.msg);
    sgw.push(obj.sgw);
})

or, if you are more familiar with for loop

for(var obj of yourArray){
    fsp.push(obj.fsp);
    msg.push(obj.msg);
    sgw.push(obj.sgw);
}

Finally you can create an array as you pointed in your example

var result = [];

result.push(fsp, msg, sgw);

And the result will be

[
    [89, 59, 43, 60, 81, 34, 28, 58, 75, 41],
    [77, 91, 4, 56, 6, 1, 42, 82, 97, 18],
    [24, 34, 4, 13, 75, 34, 14, 41, 20, 38]
]

For more informations take a look at Array.forEach(), Array.push() and for...of documentations

EDIT

As you pointed in your comment, you can generate arrays dynamically creating an object like var arrays = {};. Then in forEach(), or if for...of, you need to loop over objects with a for...in loop. The variable you declare in loop's head takes the value of index, numeric for Arrays, literal for Objects. You have to do something like:

yourArray.forEach(function(obj){
    for(let index in obj){
        if(!arrays[index]) // check if property has already been set and initialized
            arrays[index] = []; // if not, it's initialized

        arrays[index].push(obj[index]) // push the value into the array
    }
})

Note that Object has been treated as Array because you access its properties with a variable filled at runtime.

The result will be:

arrays = {
    fsp: [89, 59, 43, 60, 81, 34, 28, 58, 75, 41],
    msg: [77, 91, 4, 56, 6, 1, 42, 82, 97, 18],
    sgw: [24, 34, 4, 13, 75, 34, 14, 41, 20, 38]
}

To obtain only arrays use Object.values().

If you cannot imagine how this works, I suggest you to make some examples in Chrome Developer Tools' console, or in Node's console, or wherever you can have a realtime feedback, putting in the middle of code some console.log() of variables

2 Comments

Thank you for tour answer. Yes I already done that, but on a smaller graph, for a bigger graph it would be useful to create each array in a loop and then directly add data.
@Amoos51 Edited my answer to include what you asked in 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.