Làm phẳng mảng dùng phương pháp đệ quy

flat_array,tipjs

Làm phẳng mảng dùng phương pháp đệ quy

let result = []
let flatten = function (arr) {
  for (let i = 0; i < arr.length; i++) {
    let item = arr[i]
    if (Array.isArray(arr[i])) {
      flatten(item)
    } else {
      result.push(item)
    }
  }
  return result
}
let arr = [1, 2, [3, 4], [5, [6, 7]]]
console.log(flatten(arr));// [1,2,3,4,5,6,7]

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

using Typescript callback style error handling

Spread Operator for Objects

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

Hàm lày ngày trong tuần javascript

Hàm lày ngày trong tuần javascript

promise in javascript

promise in javascript

Tránh viết biến có phạm vi global

Đôi khi bạn cần phải viết một tập lệnh trực tiếp trên trang. Cần lưu ý rằng trong code của bạn vô tình khai báo một biến toàn cầu. Vì phạm vi toàn cầu phức tạp hơn nên việc tìm kiếm chậm hơn rất nhiều