forEach is not a function error with JavaScript array

前端 未结 11 568
梦毁少年i
梦毁少年i 2020-11-29 17:11

I\'m trying to make a simple loop:

const parent = this.el.parentElement
console.log(parent.children)
parent.children.forEach(child => {
  console.log(chil         


        
11条回答
  •  既然无缘
    2020-11-29 17:50

    parent.children will return a node list list, technically a html Collection. That is an array like object, but not an array, so you cannot call array functions over it directly. At this context you can use Array.from() to convert that into a real array,

    Array.from(parent.children).forEach(child => {
      console.log(child)
    })
    

提交回复
热议问题