Get JSON object length

Source: Internet
Author: User
Tags hasownproperty

JSON objects vary and are very flexible, and the corresponding fetch methods are:

1. The simplest type (MyObject is an object, not a string oh)

<script type= "Text/javascript" >    var myObject = {' name ': ' Kasun ', ' address ': ' Columbo ', ' Age ': ' $ '}    var Count = Object.keys (myObject). Length    Console.log (count);</script>

In older browsers, you need to include the following code to make sure you can use:

if (! Object.keys) {    Object.keys = function (obj) {        var keys = [],            K;        for (k in obj) {            if (Object.prototype.hasOwnProperty.call (obj, k)) {                keys.push (k);            }        }        return keys;    };}

The form of the value returned by the 2.ajax request:

{"Reqstatus": true, "Phones": {"one": {"number": "Xxxxxxxxxx", "type": "Mobile"}, "one": {"number": "Xxxxxxxxxx", "type": "Mobile"}}}

Traverse it:

var key, Count = 0;for (key in Data.phones) {  if (Data.phones.hasOwnProperty (key)) {    count++;  }}

3. More Methods StackOverflow

Http://stackoverflow.com/questions/6756104/get-size-of-json-objecthttp://stackoverflow.com/questions/126100/ how-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-javascript/4889658#4889658

Get JSON object length

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.