除了运行循环并使用计数器之外,有没有一种方法来查找JavaScript对象中子元素的数量?如果有助于解决问题,我可以利用jQuery。我正在这样做:
有没有一种方法可以在不使用循环和计数器的情况下查找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);
}
childScenesObj
,然后将其推送到自身。我认为这样做不会奏效。您需要在其中添加另一个var
。 - Soatl