1

I wan't to find the number of records in a keyed json object in jquery.

For example if I have an indexed object as follows

var json = [{"key1":{"value":"33"},"key2":{"value":"36 "},"key3":{"value":"38"},"key4":{"value":"41"}}]

How could I get a count of the number of records? (their are 4 records)

4
  • json.length returns undefined Commented Aug 7, 2015 at 8:07
  • the format of json variable is invalid Commented Aug 7, 2015 at 8:08
  • I have just corrected that Commented Aug 7, 2015 at 8:09
  • Check this Object.keys(json[0]).length Commented Aug 7, 2015 at 8:10

1 Answer 1

1

Use Object.keys() to get the array of all the keys in object.

In your case, json array contains an object. So, you can use json[0] to get the object from array and then use Object.keys() on it.

Object.keys(json[0]).length

var json = [{
  "key1": {
    "value": "33"
  },
  "key2": {
    "value": "36 "
  },
  "key3": {
    "value": "38"
  },
  "key4": {
    "value": "41"
  }
}];

document.write(Object.keys(json[0]).length);

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.