Matt Biggs
Matt Biggs

Reputation: 177

Length of Array within Array

I have an object that I am pushing some ints, strings and arrays into an array. And I want to get the length of the array that is within said array.

This is my code

var all_categories = [];

        all_categories.push({
            title: theTitle,
            id: theId,
            sub: subcategories
        });

Now I know that all_categories.length is the general way of getting the length and I believe that I can't run all_categories[0].sub[0].length will not work because the function does not exist.

Suggestions for a solution or work around?

Upvotes: 0

Views: 59

Answers (2)

Alexandru Severin
Alexandru Severin

Reputation: 6218

In your statement all_categories[0].sub[0].length refers to the length of the first element of array named sub.

In order to see length of the array you should call:

all_categories[0].sub.length

Upvotes: 2

Quentin
Quentin

Reputation: 943100

Assuming that subcategories is the array you want the length of, take out the second [0]. You aren't trying to get the length of the first subcategory, you're trying to get the number of subcategories.

Upvotes: 1

Related Questions