forEach不是JavaScript数组的函数错误

ale*_*nco 104 javascript ecmascript-6 vue.js

我想做一个简单的循环:

const parent = this.el.parentElement
console.log(parent.children)
parent.children.forEach(child => {
  console.log(child)
})
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

VM384:53未捕获的TypeError:parent.children.forEach不是函数

尽管parent.children日志:

在此输入图像描述

可能是什么问题呢?

注意:这是一个JSFiddle.

Dmi*_*tin 92

parent.children是一个类似于Array的对象.使用以下解决方案:

const parent = this.el.parentElement;
console.log(parent.children);
Array.prototype.forEach.call(parent.children, child => {
  console.log(child)
});
Run Code Online (Sandbox Code Playgroud)

parent.childrenIS NodeList类型,就像对象,因为数组:

  • 它包含length属性,表示节点数
  • 每个节点都是一个带有数字名称的属性值,从0开始: {0: NodeObject, 1: NodeObject, length: 2, ...}

请参阅本文中的更多详细信息.

  • 您使用哪个浏览器,以便parent.children告诉您它是一个nodeList。在 Firefox 上,它告诉我是一个 HTMLCollection。如果它是一个nodeList,.forEach() 就可以工作 (2认同)

mad*_*ox2 82

parent.children不是数组.它是HTMLCollection,它没有forEach方法.您可以先将其转换为数组.例如在ES6中:

Array.from(parent.children).forEach(child => {
    console.log(child)
});
Run Code Online (Sandbox Code Playgroud)

或使用传播运营商:

[...parent.children].forEach(function (child) {
    console.log(child)
});
Run Code Online (Sandbox Code Playgroud)

  • 我更喜欢这个解决方案,而不是搞乱Array原型. (9认同)

Raj*_*amy 16

parent.children将返回一个节点列表,技术上是一个html元素集合.这是一个类似于对象的数组,但不是数组,所以你不能直接调用它上面的数组函数.在此上下文中,您可以使用Array.from()它将其转换为真实数组,

Array.from(parent.children).forEach(child => {
  console.log(child)
})
Run Code Online (Sandbox Code Playgroud)


Jea*_*ean 10

一个更天真的版本,至少你确定它可以在所有设备上运行,没有转换和ES6:

const children = parent.children;
for (var i = 0; i < children.length; i++){
    console.log(children[i]);
}
Run Code Online (Sandbox Code Playgroud)

https://jsfiddle.net/swb12kqn/5/

  • 赞成,因为所有这些新的 ES6 函数都做与可用的旧功能完全相同的事情,但是以一种混乱的方式,就像 JS 一样 (5认同)
  • 这是更好的解决方案。它与其他编程语言很接近,并且不太可能出现奇怪的 JS 怪异现象。很简单,没有什么时髦的东西 (2认同)

Dmi*_*riy 7

parent.children是一个HTMLCollection类似于数组的对象.首先,您必须将其转换为真实Array使用Array.prototype方法.

const parent = this.el.parentElement
console.log(parent.children)
[].slice.call(parent.children).forEach(child => {
  console.log(child)
})
Run Code Online (Sandbox Code Playgroud)

  • 或者不转换它,但在.forEach()上使用.call()? (2认同)

Arm*_*oot 7

没有必要的forEach,你可以遍历仅使用from的第二个参数,如下所示:

let nodeList = [{0: [{'a':1,'b':2},{'c':3}]},{1:[]}]
Array.from(nodeList, child => {
  console.log(child)
});
Run Code Online (Sandbox Code Playgroud)


Elh*_*ony 6

如果你试图循环NodeList这样的:

const allParagraphs = document.querySelectorAll("p");
Run Code Online (Sandbox Code Playgroud)

我强烈建议以这种方式循环:

Array.prototype.forEach.call(allParagraphs , function(el) {
    // Write your code here
})
Run Code Online (Sandbox Code Playgroud)

就我个人而言,我尝试了几种方法,但大多数方法都不起作用,因为我想循环 a NodeList,但是这个方法就像一个魅力,试一试!

NodeList不是一个数组,但是我们把它当成一个数组,使用Array.所以,你要知道旧浏览器不支持它!

需要更多信息NodeList?请阅读它关于 MDN 的文档

  • 这个答案显然适用于nodeList。问题是parent.children返回一个HTML Collection,它不是一个nodeList...... (2认同)

Abd*_*hed 6

您可以检查是否正确键入了forEach,如果您像在其他编程语言中一样键入foreach,它将不起作用。


Amm*_*san 5

那是因为它parent.children是一个NodeList,并且它不支持该.forEach方法(因为NodeList是一个类似于结构但不是数组的数组),所以尝试通过首先将它转换为数组来调用它

var children = [].slice.call(parent.children);
children.forEach(yourFunc);
Run Code Online (Sandbox Code Playgroud)