selectChildByAttrbuteValue(attribute, value)
{
if(this.childNodes.length!=0)
{
for(var i=0; i<this.childNodes.length; i++)
{
if(typeof this.childNodes[i].attributes[attribute]!="undefined")
{
if(this.childNodes[i].attributes[attribute]==value)
{
return this.childNodes[i];
}else
{
this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
}
}else
{
this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
}
}
}
}
此代码返回未定义,而当我做console.log(this.childnodes [i])时;记录我的对象。然后我应该返回它..但是它返回了未定义的!
全班
class Node
{
constructor(nodeName, nodeType)
{
this.nodeName = nodeName;
this.nodeType = nodeType;
this.attributes = {};
this.childNodes = [];
this.parentNode = null;
}
removeChild(node)
{
if(node.parentNode!=null)
{
for(var i=0; i<this.childNodes.length; i++)
{
if(node == this.childNodes[i])
{
this.childNodes.splice(i, 1);
node.parentNode = null;
}
}
}
}
appendChild(child)
{
if(child.parentNode==null)
{
this.childNodes.push(child);
child.parentNode = this;
}else
{
child.parentNode.removeChild(child);
this.childNodes.push(child);
child.parentNode = this;
}
}
selectChildByAttrbuteValue(attribute, value)
{
if(this.childNodes.length!=0)
{
for(var i=0; i<this.childNodes.length; i++)
{
if(typeof this.childNodes[i].attributes[attribute]!="undefined")
{
if(this.childNodes[i].attributes[attribute]==value)
{
return this.childNodes[i];
}else
{
this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
}
}else
{
this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
}
}
}
}
}
一个完整的班级,可以看到ghow pagry it loops。
Xml_Node {
nodeName: 'row',
nodeType: 'XML_ELEMENT',
attributes: { name: 'Lee Pellion', characterID: '93746314' },
childNodes: [],
parentNode:
Xml_Node {
nodeName: 'rowset',
nodeType: 'XML_ELEMENT',
attributes:
{ name: 'characters',
key: 'characterID',
columns: 'name,characterID' },
childNodes: [ [Circular] ],
parentNode:
Xml_Node {
nodeName: 'result',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [Object],
parentNode: [Object],
innerText: '' },
innerText: '' },
innerText: '' }
根对象
Xml_Node {
nodeName: 'root',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes:
[ Xml_Node {
nodeName: 'currentTime',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [],
parentNode: [Circular],
innerText: '2016-12-06 01:20:09' },
Xml_Node {
nodeName: 'result',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [Object],
parentNode: [Circular],
innerText: '' },
Xml_Node {
nodeName: 'cachedUntil',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [],
parentNode: [Circular],
innerText: '2017-01-06 01:20:09' } ],
parentNode: null,
innerText: '' }
使用控制台日志起作用,但随后对象只是在某个地方消失!
selectChildByAttributeValue(attribute, value) {
if (this.childNodes.length != 0) {
for (var i = 0; i < this.childNodes.length; i++) {
if (typeof this.childNodes[i].attributes[attribute] != 'undefined') {
if (this.childNodes[i].attributes[attribute] == value) {
console.log(this.childNodes[i]);
return this.childNodes[i];
} else {
return this.childNodes[i].selectChildByAttributeValue(attribute, value);
}
} else {
return this.childNodes[i].selectChildByAttributeValue(attribute, value);
}
}
}
编辑:
正如我们在评论中所说的那样,您为我提供了更多信息,我想出了以下内容:
-
该功能应如下:
function selectChildByAttributeValue(attribute, value) { if (this.childNodes.length != 0) { for (var i = 0; i < this.childNodes.length; i++) { if (typeof this.childNodes[i].attributes[attribute] != 'undefined') { if (this.childNodes[i].attributes[attribute] == value) { return this.childNodes[i]; } else { return this.childNodes[i].selectChildByAttributeValue(attribute, value); } } else { return this.childNodes[i].selectChildByAttributeValue(attribute, value); } } } }
有了返回语句,因此它可以正确返回嵌套的孩子。
-
您的主要节点没有任何孩子,这就是为什么它返回
undefined
。
我在终端中进行了以下操作,然后回到了正确的节点。const n = new Node('row', 'XML_ELEMENT'); n.appendChild(new Node('rowset', 'XML_ELEMENT')); n.appendChild(new Node('rowset', 'XML_ELEMENT')); n.childNodes[0].attributes = {name: 'characters', key: 'characterID', columnds: 'name, characterID'}; n.selectChildByAttributeValue('name', 'characters');
以下结果:
Node { nodeName: 'rowset', nodeType: 'XML_ELEMENT', attributes: { name: 'characters', key: 'characterID', columnds: 'name, characterID' }, childNodes: [], parentNode: Node { nodeName: 'row', nodeType: 'XML_ELEMENT', attributes: {}, childNodes: [ [Circular], [Object] ], parentNode: null } }
我的根对象(上面称为n
)如下:
Node {
nodeName: 'row',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes:
[ Node {
nodeName: 'rowset',
nodeType: 'XML_ELEMENT',
attributes: [Object],
childNodes: [],
parentNode: [Circular] },
Node {
nodeName: 'rowset',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [],
parentNode: [Circular] } ],
parentNode: null }
旧答案
您的函数循环通过自己找到某些东西,但只有1个返回语句。
因此,当它称自己为儿童价值观并返回某些东西时,它也不存储在变量或返回中,因此它被宣告了。
您可以通过在嵌套呼叫之前放置return
来解决此问题。
因此,这两个this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
都应是return this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
。
另外,没有默认值,因此,如果您正在寻找一个值,但是单个属性没有该值,您也会得到undefined
。
小注意:您当前允许在if
语句中接受该值null
,这可能是不必要的行为。