How to use the spread operator to flatten array of objects in javascript

es6,spread_operator_javascript

How to use the spread operator to flatten array of objects in javascript

function flatten(arr) {
  while (arr.some((item) => Array.isArray(item))) {
    arr = [].concat(...arr)
  }
  return arr
}
let arr = [1, 2, [3, 4], [5, [6, 7]]]
console.log(flatten(arr))

Có thể bạn đã miss một số snippets code

Calculate the number of difference days between two dates

Calculate the number of difference days between two dates

Spread Operator for Objects

Using the spread operator during an Object declaration will assign the properties of the referenced Object to the new Object.

Đếm số phần tử trong mảng JavaScript

Đếm số lần xuất hiện của các phần tử trong mảng JavaScript

using async/await to catch errors

Remove array of objects from another array of objects

Remove array of objects from another array of objects