To count the number of objects in object literal using Jquery

前端 未结 3 1672
旧巷少年郎
旧巷少年郎 2021-02-06 04:45

Code:

var animals = {
                    \"elephant\": {
                                    \"name\" : \"Bingo\",
                                    \"age\" :         


        
3条回答
  •  渐次进展
    2021-02-06 05:28

    If you want something cross browser, that is also working on IE8, you can't do it in a really clean way (see compatibility of the keys property).

    I suggest this :

    var n = 0;
    for (var _ in animals) n++;
    

    (as it is an object literal, no need for hasOwnProperty)

提交回复
热议问题