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
serialize Form
Sử dụng formdata constructor để convert form, FormData, array.from(). Kết hợp window.encodeURIComponent() để encode từng value trong form.
Await in a for loop
How to Await in a for loop
Đế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
Rest Operator for Objects
Now we can use rest on the properties of an Object. It allows us to explicitly extract certain named variables, and assign any uncalled variables into a catchall Object.
Filter Unique Values
Kiểu đối tượng Set đã được giới thiệu trong ES6 và cùng với ..., ‘spread’ operator, chúng ta có thể sử dụng nó để tạo một mảng mới chỉ với các giá trị duy nhất.