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

Promise + gì

promise + gì

Getting the last element of a split string array

Getting the last element of a split string array

JavaScript: async/await with forEach

JavaScript: async/await with forEach()

using Typescript callback style error handling

Đế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