Merge Array

tipjs,array

How to combine two sorted arrays into one? We can do this easily with the spread operator.

a1 = [1, 2, 5, 6, 9];
a2 = [3, 4, 7, 8, 10];

res = [...a1, ...a2]; // [1, 2, 5, 6, 9, 3, 4, 7, 8, 10]

But if we want to combine and sort? Again, nothing complicated!

res = [...a1, ...a2].sort((a, b) => +a > +b); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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

flatMap()

Use flatMap() creates a new array with sub-array elements flattened by specified depth.

group by property javascript use reduce

group by property javascript use reduce

Copy a string to the clipboard

Copy a string to the clipboard. Only works as a result of user action (i.e. inside a click event listener).

Đếm có bao nhiêu items giống nhau trong một array

reduce javascript. Đếm có bao nhiêu items giống nhau trong một array

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.