在 JavaScript 中检测对象是否有子对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5887475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect if an Object has children in JavaScript
提问by Derek Adair
In JavaScript you can get the children of an XML node like this...
在 JavaScript 中,您可以像这样获取 XML 节点的子节点...
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.
我想知道哪些属性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);
}
}
回答by Mark Biek
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.
该hasOwnProperty()
函数检查以确保指定的属性确实存在于对象中。在此上下文中使用它可确保您不会获得任何继承的属性。
回答by PedroSouki
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.
我使用了一个只有两个级别的对象,但它可以与 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);
回答by Porco
Those are not children, You have created an associative array.
那些不是孩子,你已经创建了一个关联数组。
Access them like this:
像这样访问它们:
obj[prop1]
回答by ezmilhouse
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:
或者,如果您遇到一个用例,其中 obj 键的值可能被手动设置为 undefined:
if (obj.hasOwnProperty("prop3")) {
alert(obj["prop3"])
} else {
alert('obj key does not exist');
}