是否有任何可以返回对象长度的内置函数?
例如,我a = { 'a':1,'b':2,'c':3 }应该返回3.如果我使用a.length它返回undefined.
它可能是一个简单的循环函数,但我想知道是否有内置函数?
有一个相关的问题(JSON对象的长度) - 在选择的答案中,用户建议将对象转换为数组,这对我的任务来说不太舒服.
有没有办法找到javascript对象中的子项数,而不是运行循环和使用计数器?我可以利用jquery,如果它会有所帮助.我这样做:
var childScenesObj = [];
var childScenesLen = scenes[sceneID].length; //need to find number of children of scenes[sceneID]. This obviously does not work, as it an object, not an array.
for (childIndex in scenes[sceneID].children) {
childSceneObj = new Object();
childSceneID = scenes[sceneID].children[childIndex];
childSceneNode = scenes[childSceneID];
childSceneObj.name = childSceneNode.name;
childSceneObj.id = childSceneID;
childScenesObj .push(childSceneObj);
}
Run Code Online (Sandbox Code Playgroud)