How do I loop through children objects in javascript?

前端 未结 6 1823
梦如初夏
梦如初夏 2020-12-24 10:36

I have this code in a function:

tableFields = tableFields.children;
for (item in tableFields) {
    // Do stuff
}

According to a console.lo

6条回答
  •  爱一瞬间的悲伤
    2020-12-24 11:15

    In the year 2020 / 2021 it is even easier with Array.from to 'convert' from a array-like nodes to an actual array, and then using .map to loop through the resulting array. The code is as simple as the follows:

    Array.from(tableFields.children).map((child)=>console.log(child))
    

提交回复
热议问题