JavaScript JSON Array length | Example code

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 filed 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:

JavaScript JSON Array length

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.