JS “tricks” thought not good or not (part 5)

Tram Ho

Hello friends,

As usual, it’s time to air to share with you the “tricks” JS tips that I picked up. With the motto “break corona, not to cultivate knowledge” , I hope you have, are and will be able to follow your series to leave comments and upvote so that I have the motivation to continue writing! And if you have not read the previous 4 sections, then try to see if your tips are any good:

[JS tips series] JS “tricks” thought not good or not

And in this term, to gradually be able to “control” JS better, I will share with you some tips that are difficult to relate to array in JS. Let’s go!

1. Check if the two arrays have the same content

A very familiar problem that I bet most of you have met at least once in your life. I will rephrase the problem as follows: “Given two arrays a with n elements and b with m elements, check if the two arrays have the same content or not”.

A little example to better understand the problem:

As usual, we will iterate over array a , with each element of our a see if it is in array b or not. If not, then stop reviewing and conclude and these 2 arrays are not the same. If not, if we have finished browsing array a , we need to do the same with the elements of array b and conclude whether the two arrays are the same or not.

It’s a bit complicated and a bit headache. But fortunately, JS has some support for us to implement this in about 30 seconds! That is using Set and filter . Let’s see:

It’s much simpler, isn’t it! Note that the array applied in this case is an array of numbers, or strings, but the array of objects is lost immediately ? . One more note is that the haveSameContents(a, b) does not check for complicated multidimensional arrays, only applies to one-dimensional arrays, multidimensional arrays, I would like to wait for the comment of the high-ranking person below the article. hey, hope you share

2. Check if this array is in the other array

Checking whether a number or string belongs to a certain array of numbers or strings is too simple. But now our problem is: “Given two arrays of a with n elements and b with m elements, check if array b has ‘contains’ array a ?”.

The way not to think that everyone knows is to use 2 nested for loops to browse and check each element one by one, right? But with JS we don’t necessarily have to make things that complicated:

The code is nicer, isn’t it? With filter and some , everything is easier to write than 2 cycles for right. But the nature is still to browse that array anyway.

3. A bit of for loops

Surely you also no stranger to for the JS, and we can close our eyes that lists the types for which:

  • for ... in
  • for ... of
  • forEach

So, what is the difference between these types of for?

3.1 for … in loop

for ... in is used to browse the properties enumerable ( enumerable properties ), including the attributes inherited from the father object. (If you are wondering what enumerable properties are, then look at part 4 for a bit, I mentioned in the first tip of part 4. ? ). for ... in also be used with string arrays or a regular object , but cannot be used with Map or Set .

3.2 For … of loop

for ... of is used to browse iterable objects , but this way of browsing is passing values rather than attributes like for ... in above.

3.3 forEach loop

Finally, the forEach loop, which is an Array prototype method, allows us to browse the elements of an array. And it can access both the value and the index of the array if you want to while browsing.

By the way, when forEach about forEach , there is a question that is asked: When browsing an array in the usual way, we give the index to run back from the length - 1 position length - 1 to position 0 , but if using forEach we Is it possible to reverse the array from the right to the right?

“Alright, reverse the array and browse and finish!”

That’s right ? , must add a reversal array before using forEach , ask a little bit of threats:

4. Capitalize & Decapitalize

Have fun at the end of the game with uppercase and lowercase letters! We all know toUpperCase() and toLowerCase() . But sometimes we don’t need to put everything in uppercase or lowercase. Typically when writing a newspaper, for example, the first word of the first word is always capitalized, then we only need to capitalize the first word only:

This capitalize() function allows us to keep the remaining characters intact or convert them all to lowercase depending on the purpose of the user.

In contrast to capitalize() , we have decapitalize() as follows:

Conclude

So we have gone through 4 “tricks” both easy and difficult of this article. I hope it brings some useful to you. If you have any tips or questions, please leave a comment that I will answer enthusiastically. And don’t forget to drop upvote, guys ? .

Thank you!

Reference: 30 seconds of code

Share the news now

Source : Viblo