0

I have a decoded Json variable called objIntChart that looks like the following when I console.log() it.

objIntChart

The problem is I need it as an array that looks like this

enter image description here

I did a foreach loop like this in an attempt to solve it:

   var array = [];

            objIntChart.forEach(function (entry) {
                var x = 0++;
                array.push(x);
                array.x.push(entry['dateTime']);
                array.x.push(entry['entries']);

            });

However the problem is that I cannot do a push on array.x because it takes the x as the name and not the variable. Is there a solution to this?

3 Answers 3

2

Keep it simple:

var array = [];
objIntChart.forEach(function (entry) {
    array.push([entry['dateTime'], entry['entries']]);
});
Sign up to request clarification or add additional context in comments.

2 Comments

For the record, with your approach you'd have needed array[x] = [], array[x].push(..).
Doh I am stupid. I actually did something along the lines of that first but then I just went completely off track. Anyways thanks for the response
1

This will do as well:

var arr = objIntChart.map(function (obj) { return [obj.dateTimes, obj.entries]});
console.log(arr);

Comments

0

You should use array[x] instead of array.x and also be doing array[x] = [] or array.push([]) instead of array.push(x) which will add the integer x, not an empty array, to your array.

Also, you will need to set var x = 0; outside your forloop and do x++ inside it. Currently, x will always be 1 when you use it.

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.