Arrays are an essential data structure in JavaScript, allowing you to store and manipulate multiple values in a single variable. In this blog post, we'll cover some of the basics of arrays in JavaScript.
There are different functions we can execute with arrays and I will be taking the commonly used functions and explain with examples. for the purpose of this lesson we will be using TypeScript.
The sort function:
it is used to sort elements of an array in ascending or descending manner
look at the pics below...
The filter function
Iterative methods include the filter() method. For each element in an array, it makes a single call to the given callbackFn function, which creates a new array containing all the values for which callbackFn returns a truthy result. None of the array members that fail the callbackFn test are added to the new array.
The map Function
For each element of an array, map() calls a function to generate a new array. For empty elements, the function is not run by map(). The initial array is unaffected by map().
The find Function
The first element in the given array that fulfills the testing function is returned by the find() method. Undefined is returned if no values satisfy the testing function. Use findIndex() if you require the array's found element's index.
Top comments (0)