Home > Web Front-end > JS Tutorial > body text

How to get the length of json array in js

php中世界最好的语言
Release: 2017-12-30 17:58:39
Original
3837 people have browsed it

What I bring to you this time is how to get the length of the jsonarray in js. We know that json does not have the .lengthattribute. How to get the length of the json array? This article will give you a good analysis.

The problem I encountered is this: In the page at the end of ***.jsp, the json data passed to me by the backend is an array, and then I need to get the array length in the second array contacts, Then Baidu discovered json does not have a .length attribute. How to do it?

Since the json object does not have a length attribute, what if you want to know its length?

var jslength=0;
 for(var js2 in json){
 jslength++;
 }
Copy after login

Write this code into a method and call it later:

function getJsonLength(jsonData){
 var jsonLength = 0;
 for(var item in jsonData){
  jsonLength++;
 }
 return jsonLength;
}
Copy after login

I believe you have mastered the method after reading the above introduction. Please pay attention to php for more exciting information. Chinese website Other related articles!

Related reading:

How to customize the console object during the use of JS

How to download and install nodejs

Summary of the front-end js framework and explanation of its uses

The above is the detailed content of How to get the length of json array in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!