4 Helpful Javascript Array tricks you should know

talent - Jun 1 '22 - - Dev Community

An array is one of the most common concepts of Javascript, which gives us a lot of possibilities to work with data. Check these tricks which can be very helpful!

๐Ÿ‘‰ ๐—Ÿ๐—ฒ๐˜โ€™๐˜€ ๐—ด๐—ฒ๐˜ ๐˜€๐˜๐—ฎ๐—ฟ๐˜๐—ฒ๐—ฑ ๐Ÿง‘โ€๐Ÿ’ป

๐ŸŒถ๏ธ ๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ฟ๐—ฒ๐—บ๐—ผ๐˜ƒ๐—ฒ ๐—ณ๐—ฎ๐—น๐˜€๐˜† ๐˜ƒ๐—ฎ๐—น๐˜‚๐—ฒ๐˜€ ๐—ณ๐—ฟ๐—ผ๐—บ ๐—ฎ๐—ป ๐—ฎ๐—ฟ๐—ฟ๐—ฎ๐˜† ๐—ถ๐—ป ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜.
In #javascript falsy values are false, 0, โ€ ", null, NaN, undefined. Now we can find out how to remove this kind of value from our array. To achieve this, we are going to use the .filter().๐Ÿš€

๐ŸŒถ๏ธ ๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ด๐—ฒ๐˜ ๐—ฎ ๐—ฟ๐—ฎ๐—ป๐—ฑ๐—ผ๐—บ ๐—ฒ๐—น๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ณ๐—ฟ๐—ผ๐—บ ๐—ฎ๐—ป ๐—ฎ๐—ฟ๐—ฟ๐—ฎ๐˜† ๐—ถ๐—ป ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜.
Sometimes we need to select a value from the array randomly. To create it, we can get a random index number according to the array length.๐Ÿš€

๐ŸŒถ๏ธ ๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ฆ๐—ต๐˜‚๐—ณ๐—ณ๐—น๐—ฒ ๐—ฎ๐—ป ๐—”๐—ฟ๐—ฟ๐—ฎ๐˜† ๐—ถ๐—ป ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜
Shuffling is easy with the sort method. As long as it returns a random number, positive or negative, we can sort it with a random order.๐Ÿš€

๐ŸŒถ๏ธ ๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—™๐—ถ๐—ป๐—ฑ ๐˜๐—ต๐—ฒ ๐—ถ๐—ป๐˜๐—ฒ๐—ฟ๐˜€๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐—ผ๐—ณ ๐˜๐˜„๐—ผ ๐—ฎ๐—ฟ๐—ฟ๐—ฎ๐˜†๐˜€ ๐—ถ๐—ป ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜.
We can use a Set() to ensure that values in the array we are checking are not duplicated, and we will use .filter and .includes methods. As a result, we will get the array with values that were presented in both arrays.๐Ÿš€

Image description

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player