获取array.foreach函数的返回值 [英] Getting return value for array.foreach function

查看:92
本文介绍了获取array.foreach函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好.我是JavaScript新手.我被下面的函数所困扰,试图获取一个值(dom树的一部分).我没有获得有用的值,而是获得了0/undefined.我在互联网上甚至在这里都在寻找解决方案,但找不到与我类似的案例.我该如何正确实施呢?有什么建议吗?提前非常感谢您.最好的G.

Good evening. I'm a JavaScript newbie. I'm stucked with the following function, trying to get back a value (a part of a dom tree). Instead of receiving a useful value I just obtain a 0/undefined. I looked for solutions on internet and even here, but I couldn't find a case that could resemble mine. How could I implement this correctly? Any suggestion? Thank you so much in advance. Best G.

var findNodeForAttributeValue = function (node, innerXmlAttributeName, innerXmlAttributeValue) {
var indirectReturnVar='0';
if ((node.nodeType === 1)&&(node.hasAttribute(innerXmlAttributeName))) {
    if (node.getAttribute(innerXmlAttributeName)==innerXmlAttributeValue) {
        indirectReturnVar=node;
        return indirectReturnVar;
    }
}
if((node.hasChildNodes())&&(node.firstChild!=null)) {
    Array.forEach ( node.childNodes, function (children) {
        findNodeForAttributeValue(children, innerXmlAttributeName, innerXmlAttributeValue);
    } );
    return indirectReturnVar;
}

}

更新的代码:

var findNodeForAttributeValue = function (node, innerXmlAttributeName, innerXmlAttributeValue) {

  var indirectReturnVar='0';

  if ((node.nodeType === 1) && (node.hasAttribute(innerXmlAttributeName))) {

    if (node.getAttribute(innerXmlAttributeName) == innerXmlAttributeValue) {
      indirectReturnVar = node;
      return indirectReturnVar;
    }
  }

  if ((node.hasChildNodes()) && (node.firstChild != null)) {

    for (var fi=0, fiLen=node.childNodes.length; fi<fiLen; fi++) {
      findNodeForAttributeValue(node.childNodes[fi], innerXmlAttributeName, innerXmlAttributeValue);
    }
    return indirectReturnVar;
  }
}

推荐答案

当您这样做:

> Array.forEach ( node.childNodes .. )

forEach Array.prototype 上的Array实例的方法. childNodes 属性是 NodeList ,它不是一个数组.

forEach is a method of Array instances that is on Array.prototype. the childNodes property is a NodeList, which is not an Array.

在某些支持ES5的浏览器中,您可以执行以下操作:

In some browsers that support ES5 you can do:

Array.prototype.forEach.call(childNodes, ...)

但这不能保证能正常工作(并且在IE 8及更低版本中会失败).因此,只需使用for循环:

but that isn't guaranteed to work (and will fail in IE 8 and lower). So just use a for loop:

for (var i=0, iLen=node.childNodes.length; i<iLen; i++) {
  // do stuff with node.childNodes[i];
}

编辑

要修复更新的代码,请执行以下操作:

Edit

To fix your updated code:

function findNodeForAttributeValue (node, innerXmlAttributeName, innerXmlAttributeValue) {

使用函数声明,我不明白为什么您要使用带有赋值的表达式.另外,较短的变量名将使工作变得更轻松,我可能会做类似的事情:

Use a function declaration, I don't understand why you are using expressions with assignment. Also, shorter variable names will make life a lot easier, I'd probably do something like:

function getNodeByAttributeValue (node, att, value)

如果您希望变量具有真实值,只需将其设置为true.在这种情况下,您希望它是假的,因此要么将其保留为未定义状态,要么将其设置为 null (因为大多数DOM方法在没有匹配元素的情况下都会返回null):

If you want a variable to have a truthy value, just set it to true. In this case, you want it falsey so either leave it undefined or set it to null (since most DOM methods return null if they don't get a matching element):

  var indirectReturnVar = null;

这个for块很好.

  if ((node.nodeType === 1) && (node.hasAttribute(innerXmlAttributeName))) {

    if (node.getAttribute(innerXmlAttributeName) == innerXmlAttributeValue) {
      indirectReturnVar = node;
      return indirectReturnVar;
    }
  }

  if ((node.hasChildNodes()) && (node.firstChild != null)) {

该位需要修改.仅在 indirectReturnVar 为假时保持循环:

This bit needs modifying. Only keep looping while indirectReturnVar is falsey:

    for (var fi=0, fiLen=node.childNodes.length; fi<fiLen && !indirectReturnVar; fi++) {

将递归函数的返回值分配给 indirectReturnVar ,否则它将在以太币中丢失.

Assign the returned value of the recursive function to indirectReturnVar, otherwise it gets lost in the ether.

      indirectReturnVar = findNodeForAttributeValue(node.childNodes[fi], innerXmlAttributeName, innerXmlAttributeValue);


    }
  }

在递归循环中外部返回值.它只会循环直到找到匹配的节点或节点用完为止.

Return the value outside the recursive loop. It will only loop until it either finds a matching node or runs out of nodes.

  return indirectReturnVar;
}

这篇关于获取array.foreach函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆