Use Object.keys(myObject).length method to get JSON array length in JavaScript. In the method, you have to use the data name and filed name to get the length of it.
JavaScript JSON Array length Example
HTML example code:
Counting JSON array length by id, you can also use another file for it.
<!DOCTYPE HTML>
<html>
<body>
<script>
var data = {
"shareInfo": [{
"id": "1",
"a": "sss",
"b": "sss",
"question": "whi?"
},
{
"id": "2",
"a": "sss",
"b": "sss",
"question": "whi?"
},
{
"id": "3",
"a": "sss",
"b": "sss",
"question": "whi?"
},
{
"id": "4",
"a": "sss",
"b": "sss",
"question": "whi?"
}
]};
var len = Object.keys(data.shareInfo[0]).length;
console.log(len);
</script>
</body>
</html
Output:
Do comment if you have any doubts and suggestions on this JS JSON Array topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version
My bad, you get lenght of element inside.
Why use [0] index ?
console.log(data.shareInfo.length) do the trick.