JavaScript數組扁平化處理

let testArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
function simpleNormalizeChildren (children) {
  for (let i = 0; i < children.length; i++) {
    if (Array.isArray(children[i])) {
      return Array.prototype.concat.apply([], children)
    }
  }
  return children
}
console.log(simpleNormalizeChildren(testArray))

得到:[1, 2, 3, 4, 5, 6, 7, 8, 9]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章