gpt4 book ai didi

javascript如何在一个对象中找到 child 的数量

转载 作者:数据小太阳 更新时间:2023-10-29 03:58:50 25 4
gpt4 key购买 nike

除了运行循环和使用计数器之外,是否有其他方法可以找到 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);
}

最佳答案

以下在 ECMAScript5 (Javascript 1.85) 中工作

var x = {"1":1, "A":2};
Object.keys(x).length; //outputs 2

关于javascript如何在一个对象中找到 child 的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7081165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com