简体   繁体   中英

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM