Js converts two-dimensional array objects to one-dimensional array objects

let arr = [

]
  {
    a: 1,
    b: 2,
    c: 3,
    children: [
      {
        id: 1,
        a: 1-1,
        b: 1-2,
        c: 1-3,
      }
    ]
  },
  {
    a: 2-1,
    b: 2-2,
    c: 2-3,
    children: []
  }
]

to change the data format to

clipboard.png
key
function getData (arr) {
arr.forEach((item, index) => {


})
return brr
}
getData(arr)

childrenchildrenkey

clipboard.png

Mar.21,2021

the following program is for reference, and no matter how deep the level is, it can be supported.

explain why for does not use forEach here. After the current loop judgment takes effect, for adds a subitem to arr, which will affect the next loop, while forEach is not. Originally, this feature of for is a defect, and the emergence of ES5 forEach is also to make up for this defect, but there is nothing absolute, and good use is an advantage.

  

what is the meaning of the key value of each object you require, and what are the rules?
if this rule is not clear, the data processed later may not necessarily meet your requirements.

Menu