0

I need to get an array to display the following way:

var categories = data.categories;

for(a = 0; a < data.count; a++){
    var names[a] = data.name[a];
    var values[a] = data.value[a];
}

There will be one "Categories" array, for example "May 2017","June 2017","July 2017","August 2017","September 2017". Then there may be 3/4 arrays containing "Name" and "Values" and I want these to loop through a loop to loop and assign these to variables.

At the moment I'm getting 'Undefined" in all variables.

My output in php is currently:

[
    [{
        "name": "Leads",
        "data": [524, 419, 502, 598, 873],
        "color": "#00afef"
    }, {
        "name": "Purchases",
        "data": [37, 18, 32, 36, 44],
        "color": "#f00"
    }], {
        "categories": ["May 2017", "June 2017", "July 2017", "August 2017", "September 2017"]
    }
]
4
  • You aren't using categories anywhere, that might be your first clue. Commented Mar 27, 2018 at 21:59
  • 1
    should data.count not be data.length? you don't use var on array members ("var names[a]") why are you creating a categories variable if you're not using it anywhere? Commented Mar 27, 2018 at 22:00
  • @Devon I don't want categories to loop, I only want to loop through the names and values. Commented Mar 27, 2018 at 22:04
  • you can write the answer that you expect correct from the content of names[] and values[] Commented Mar 27, 2018 at 22:09

1 Answer 1

1

The JSON you're getting back has horrible schema and you're doing weird things in JS. Hopefully the following will get you back on track:

var data = [
  [{
    "name": "Leads",
    "data": [524, 419, 502, 598, 873],
    "color": "#00afef"
  }, {
    "name": "Purchases",
    "data": [37, 18, 32, 36, 44],
    "color": "#f00"
  }], {
    "categories": ["May 2017", "June 2017", "July 2017", "August 2017", "September 2017"]
  }
];

var categories = data[1].categories;

var names = [];
var values = [];

for (var a = 0; a < data[0].length; a++) {
  names.push(data[0][a].name);
  values.push(data[0][a]);
}

console.log(categories);
console.log(names);
console.log(values);

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

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.