Javascript 获取对象中数组的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5861536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:13:42  来源:igfitidea点击:

Getting the size of an array in an object

javascriptarraysobjectcount

提问by meli

I would like some help with getting the size of an array inside an object:

我想要一些关于获取对象内数组大小的帮助:

var st = { "itema":{...},"itemb":[{"id":"s01","cd":"c01","dd":"d01",....}{"id":"s02","cd":"c02","dd":"d02",....}]}

How would you get a count of the objects inside "itemb"(in this case 2)?

您将如何获得内部对象的数量"itemb"(在本例中为 2)?

回答by Andrew Hare

Javascript arrays have a length property. Use it like this:

Javascript 数组有一个 length 属性。像这样使用它:

st.itemb.length

回答by BrunoLM

Arrays have a property .lengththat returns the number of elements.

数组具有.length返回元素数量的属性。

var st =
    {
        "itema":{},
        "itemb":
        [
            {"id":"s01","cd":"c01","dd":"d01"},
            {"id":"s02","cd":"c02","dd":"d02"}
        ]
    };

st.itemb.length // 2