1

I have got this JSON

var data =
[
    {
        "new": [
            {
                "screen": "SCR2",
                "time_of_order": "20:08:58",
                "status": "Viewed",
                "item_id": "9,6,5,4",
                "quantity": "1",
                "vendor_id": "1",
                "order_id": "140827080458858O1O1",
                "json_value": [
                    {
                        "SurCharges": "50"
                    },
                    {
                        "SurCharges": "50"
                    },
                    {
                        "SurCharges": "50"
                    }
                ],
                "seat_number": "D12"
            }
        ]
    }
];

How to get the length of a nested array named json_value ??

This is my jsfiddle

http://jsfiddle.net/03zw041h/2/

Tried using

alert(data.new.json_value.length);

But console is reporting an error

Uncaught TypeError: Cannot read property 'json_value' of undefined 

3 Answers 3

2

You have some arrays in there

data[0].new[0].json_value.length 

FIDDLE

Sign up to request clarification or add additional context in comments.

1 Comment

Ya beat me to it, next time I'll forego the fiddle :P
1

You have nested arrays in there as well so you have to account for those.

http://jsfiddle.net/03zw041h/4/

data[0].new[0].json_value.length

2 Comments

Thank you very much , but i am facing issues when it comes to real time data in my project (more nested arrays present)so Is it possible to get the count of SurCharges present under the json_value??
If you're asking how to loop through those values and add them together you're better off posting another question, or just google arrays and looping.
0

You have arrays of objects with only 1 element in each one, so maybe you don't want/need them as arrays:

var data =
{
    "new":
    {
        "screen": "SCR2",
        "time_of_order": "20:08:58",
        "status": "Viewed",
        "item_id": "9,6,5,4",
        "quantity": "1",
        "vendor_id": "1",
        "order_id": "140827080458858O1O1",
        "json_value":
        [
            {
                "SurCharges": "50"
            },
            {
                "SurCharges": "50"
            },
            {
                "SurCharges": "50"
            }
       ],
       "seat_number": "D12"
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.