在 JavaScript 中检测对象是否有子对象

发布于 2024-11-05 02:38:12 字数 1144 浏览 1 评论 0原文

在 JavaScript 中,您可以像这样获取 XML 节点的子节点...

var children = xml.childeNodes;

如何获取对象的子节点?

var obj = {
  prop1: 'stuff',
  prop2: 'things',
  prop3: 'stuff-n-things'
}

第二轮

给定一个像这样的对象..

var Obj = {
  levelOneProp1: 'stuff',
  levelOneProp2: 'things',
  levelOneProp3: {
     levelTwoProp1: 'moreStuff',
     levelTwoProp2: 'morethings',
     levelTwoProp3: 'morestuff-n-things'
  } 
}

我想知道 Obj 中的哪些属性有子项,以便我可以以递归方式循环遍历它们。目标是能够提供一个具有(理论上)无限数量的子项的数据集,并将它们的值应用于输入元素......这是我到目前为止所拥有的。

function applyData( dataSet ){
    var hasChildren = false;

    for(var i = 0; i < dataSet.childNodeArrayGoesHere.length; i++){
        if(dataSet.detectChildNodesHere){
            hasChildren = true;
        }
    }

    if(hasChildren){
        for(var j = 0; j < dataSet.childNodeArrayGoesHere.length; i++){
            applyData(dataSet[j]);
        }
    } else {
        //apply the key/value pair to an input element

        $("input[name" + dataSet.propertyName + "]").val(dataSet.propertyValue);
    }
}

In JavaScript you can get the children of an XML node like this...

var children = xml.childeNodes;

How do I get the children of an object?

var obj = {
  prop1: 'stuff',
  prop2: 'things',
  prop3: 'stuff-n-things'
}

Round two

Given an object like so..

var Obj = {
  levelOneProp1: 'stuff',
  levelOneProp2: 'things',
  levelOneProp3: {
     levelTwoProp1: 'moreStuff',
     levelTwoProp2: 'morethings',
     levelTwoProp3: 'morestuff-n-things'
  } 
}

I would like to know which properties in Obj have children so I can loop through them in a recursive manner. The goal is to be able to supply a dataset with an (theoretically) unlimited number of children and apply their values to input elements... Here is what I have so far.

function applyData( dataSet ){
    var hasChildren = false;

    for(var i = 0; i < dataSet.childNodeArrayGoesHere.length; i++){
        if(dataSet.detectChildNodesHere){
            hasChildren = true;
        }
    }

    if(hasChildren){
        for(var j = 0; j < dataSet.childNodeArrayGoesHere.length; i++){
            applyData(dataSet[j]);
        }
    } else {
        //apply the key/value pair to an input element

        $("input[name" + dataSet.propertyName + "]").val(dataSet.propertyValue);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

又怨 2024-11-12 02:38:12

您可以像这样迭代对象的所有属性:

var o = { prop1: 'this is prop 1', prop2: 'this is prop2'};

for(var propName in o) {
    if(o.hasOwnProperty(propName)) {
        alert(o[propName]);   
    }
}

hasOwnProperty() 函数检查以确保指定的属性实际上存在于对象中。在这种情况下使用它可以确保您不会获得任何继承的属性。

You can iterate over all properties of an object like this:

var o = { prop1: 'this is prop 1', prop2: 'this is prop2'};

for(var propName in o) {
    if(o.hasOwnProperty(propName)) {
        alert(o[propName]);   
    }
}

The hasOwnProperty() function checks to make sure that the specified property actually exists in the object. Using it in this context makes sure you don't get any inherited properties.

顾冷 2024-11-12 02:38:12

您可以像这样递归地迭代对象的所有属性。

找到所有的结束节点,按名称选择对应的输入标签,并将值放入其中。

我使用的对象只有两个级别,但它可以与 n 个级别的对象一起使用。

var Obj = {
          id1: 'stuff',
          id2: 'things',
          id3: {
              levelTwo1: 'moreStuff',
              levelTwo2: 'morethings',
              levelTwo3: 'morestuff-n-things'
          }
      }

function addAttributeInput(obj) {
    for (var o in obj) {
        if (typeof obj[o] == "object") {
            addAttributeInput(obj[o]);
        } else {
            $("input[name='" + o + "']").val(obj[o]);
        }
    }
}

addAttributeInput(Obj);

You can iterate over all properties of an object recursively like this.

Find all the end nodes, select the corresponding input tags by the name and put the value in it.

I used an object with just two levels, but it can be used with an object of n levels.

var Obj = {
          id1: 'stuff',
          id2: 'things',
          id3: {
              levelTwo1: 'moreStuff',
              levelTwo2: 'morethings',
              levelTwo3: 'morestuff-n-things'
          }
      }

function addAttributeInput(obj) {
    for (var o in obj) {
        if (typeof obj[o] == "object") {
            addAttributeInput(obj[o]);
        } else {
            $("input[name='" + o + "']").val(obj[o]);
        }
    }
}

addAttributeInput(Obj);
睫毛溺水了 2024-11-12 02:38:12

此代码检测 obj 是否有任何子项

Object.keys(obj).length

This code detect whether obj has any child

Object.keys(obj).length
柏拉图鍀咏恒 2024-11-12 02:38:12

那些不是孩子,您已经创建了一个关联数组。

像这样访问它们:

obj[prop1]

Those are not children, You have created an associative array.

Access them like this:

obj[prop1]
梦萦几度 2024-11-12 02:38:12

您正在寻找的实际上是对象键。您可以通过调用它来检查对象键是否存在:

您的对象:

var obj = {
    prop1: 1,
    prop2: 2
}

检查是否未定义:

if (obj.prop3 === undefined) {
    alert('obj key does not exist');
} else {
    alert(obj.prop3);
}

if (obj["prop3"] === undefined) {
    alert('obj key does not exist');
} else {
    alert(obj["prop3"]);
}

或者如果您遇到以下用例: obj 键的值可能会手动设置为未定义:

if (obj.hasOwnProperty("prop3")) {
    alert(obj["prop3"])
} else {
    alert('obj key does not exist');
}

What you're looking for are actually object keys. You can check if a object key exists by calling it:

your obj:

var obj = {
    prop1: 1,
    prop2: 2
}

check if undefined:

if (obj.prop3 === undefined) {
    alert('obj key does not exist');
} else {
    alert(obj.prop3);
}

or

if (obj["prop3"] === undefined) {
    alert('obj key does not exist');
} else {
    alert(obj["prop3"]);
}

or if you face a use case where a obj key's value might be set to undefined manually:

if (obj.hasOwnProperty("prop3")) {
    alert(obj["prop3"])
} else {
    alert('obj key does not exist');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文