Some useful Javascript functions about Array and Object

Tram Ho

1. filter ()

Create a new array based on items from the old table through certain filter conditions:

For example:

Create an array of aged students drinking alcohol:

2. map ()

Create a new array by customizing values ​​from another array.

For example:

Create an array and prepend each element with the $ symbol.

3. reduce ()

Diminishes all elements in the array to a single value.

The return value can be any type (object, array, string, integer).

For example:

Calculate the total value in the array

In addition reduce() can also group objects by an attribute, delete duplicated item , flatten an array, …

4. forEach ()

Performs the same job for each element in the array.

For example:

Print out the elements of the array

5. some ()

Browse the elements in the array, when there are elements that meet the conditions will end the loop and return true

6. every ()

Same as with some() , but only returns true when all properties in the array meet the condition.

7. includes ()

Checks whether the array contains the input element, return true if the array contains that element.

For example:

Checks if its function contains the word waldo

8. Array.from ()

Create a new array based on another array or string.

For example:

Create an array from a string

Create a new array that has double value for the corresponding value of the given array.

9. Object.values ​​()

Returns an array of Object values.

For example:

10. Object.keys ()

Returns an array of object key

For example:

11. Object.entries ()

Returns an array that contains the object’s keyvalue pairs

For example:

12. Object.freeze ()

Prevent editing of existing properties or adding properties of the object

People often think that the keyword const can do that, however, const still allows you to edit the object

For example:

Prevent the editing of the name attribute

13. Object.seal ()

Prevent adding new properties to the object, but can still modify existing properties.

For example:


This is the end of my article, hopefully as useful as the title for you programmers. Thank you for your interest !!


Share the news now

Source : Viblo