0

I have this json array:

var a = [{"results":[{"id":"25","name":"John","age":"46"},{"id":"41","name":"Sonny","age":"31"}],"count":2,"total":14}];

It contains an array called "results" and two other variables with numerical values, count and total.

How can i get each of the values of "results", "count" and "total" from the above array ?

I tried: console.log(a.count);

But it says undefined.

2 Answers 2

2

To load results:

console.log(a[0].results);

To load count:

console.log(a[0].count);

To load total:

console.log(a[0].total);
Sign up to request clarification or add additional context in comments.

1 Comment

P.S results is an object, so you will have to load whatever element of result you need like so: console.log("results id: " + a[0].results[0].id);
1

First, execute your code will cause SyntaxError: Unexpected token ILLEGAL.

There is an invalid character after age between "age"‌​:"31".

Remove that, then a is an array, so you get the first element by a[0],

then

get the count by a[0].count

get the results by a[0].results

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.