A few tips when using arrays in JavaScript not everyone knows

Tram Ho

1, Delete duplicate elements in array

In the second way, surely many of you will be wondering what the hell is [...new Set(fruits)] what the … (…) in this code is. Actually it is just a fairly new function in ES6 called Spread Operator , if you have ever been in contact with Ruby, it will be quite similar to Splat Operator . The Spread Operator allows converting a string into multiple arguments (in case of calling with the function) or into multiple elements (for arrays).
Visual example for you to understand.

In addition this Spread Operator also has some pretty good effects, you learn yourself offline.

2, Replace the values ​​in the array

The splice () method adds / deletes items into an array and returns deleted items. splice(start, value to remove, valueToAdd)

3, Loop in the array

For example, there is an array:

Now want to retrieve an array containing the name, normally many of you will do the usual way that is using the for loop as follows.

There is a faster way to use .map() .

Faster, isn’t it.
The map () function accepts three parameters (in order):

  • The current element of the array.
  • The index of the current element in the array.
  • Original array.

A similar way to the .map() method is the .from() method.

4, Return an empty array

You have a full array of elements but you need to “clean” it for any purpose and you don’t want to delete each one individually? So easy

5, Convert array into object

If you want to use an object instead of a simple array type, you can use the Spread Operator I just introduced.

Otherwise you can use the following:

These are both ways to use syntax in ES6.

6, Create an array of the same values

Here I introduce to you a method that I also know is the method .fill()

7, join the array

There are many ways to combine arrays, such as using the for loop, then pushing the test, or using the .concat() function. But one way I mentioned above is to use the Spread Operator .

8, Find duplicate elements in 2 given arrays.

Here using the .filter() method .filter() that you will return an array of values ​​under certain conditions. And the .includes() method simply checks to see if the value of an element is in the array.

9, Remove those values ​​according to conditions

You simply need to use the .filter() function as I explained above.

10, Get random 1 element in the array

11, Reverse the array

When you want to reverse an array, if you’re a beginner you will use the for loop to then browse from the last element to the first. But to make it simpler javascript gave birth to a short method called reverse() .

12, Find the last occurrence of an element in the array.

Just use lastIndexOf() finish.

13, Calculate the total value in the array

The easiest way is to use the for loop. For example

This is a common use, and one more way is to use the .reduce() method. You can use the function from the values ​​available in the array to create a completely new value. As here is the sum

.reduce() can take multiple parameters.

  • The first parameter is the initial value, usually we will lightning this value at the end of the function
  • The second parameter is the current element in the array

Conclude

Above are some ways for you to easily use arrays in javascript.

References here

Share the news now

Source : Viblo