0

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?

1
  • what you want, the total subcategories or only within one all_category Commented Dec 8, 2014 at 11:41

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

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.