JS gets the length of the array inside the JSON array

Source: Internet
Author: User
Tags array length

As a front-end page developer for the first time to process JSON data, encountered the ' JS gets the length of the array inside the JSON array '? I don't know if JSON has no. Length property (really want to taunt yourself), an idle youth needy age AH! I used to seek help from my boyfriend, but recently tried to solve the problem.

My problem is this: ***.jsp end of the page, the back end of the JSON data passed to me is an array, and then to get the second array contacts inside the array length, and then Baidu found that JSON does not. Length property. What do we do?

Since the JSON object has no length property, what if you want to know what his length is?

var jslength=0;  for (var in json) {   jslength+ +;    }

Write this code in a way that you can call later:

function Getjsonlength (jsondata) {    var0;      for (var in jsondata) {       jsonlength+ +;    }     return jsonlength;}    

But the above method can only get to the length of the first level array??? Cannot get the array length inside the Subarray??? Then self-played, using the following method to obtain the length of the contacts array successfully.

var_data =${contactjson};function getjsonlength (jsondata) {varJsonlength =0; for(varIteminchjsondata) {if(Item = ='Contacts'){ for(varXinchJsondata[item]) {Jsonlength++; }}}returnjsonlength;}var_contact_num =getjsonlength (_data); $ ('#contactNum'). Text (_contact_num);

The result of the last call:

JS gets the length of the array inside the JSON array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.