JavaScript - Arrays
JavaScript
Arrays
Today’s topic JavaScript arrays and some of its most used methods.
What are Arrays?
Arrays re used to store more than one value at a time Arrays are special variables.
Syntax: const array_name = [n0 ,n1 ,n2 ,n3 ,nn];
Eg: This is the basic way for creating an array.
Types of arrays
(1) Uniform - An array which has a single type of data.
Eg - Uniform array.
(2) Un uniform - An array which has multiple types of data.
Eg - Un uniform array.
(2) Dimensional Array - A simple description would be the 2D Array are Arrays of Arrays.
Eg - 2D array.
The Array.from( ):
It takes an array-like object & turns it into a complete array, or you can say does a shallow copy. In which you can perform all array functions.
Syntax : Array from (arrayLike, mapfn, thisarg)
arrayLike
An array-like or iterable object to convert to an array.
mapFn Optional
Map function to call on every element of the array.
thisArg Optional
Value to use as this when executing mapFn.
Eg - Array.from( ).
Array manipulation methods
(important one's)
(1) push( ): adds a data item to the array at the end index.
Eg - push( )
(2) pop( ) : remove a data item from the last index of an array & also returns the popped Value which you can store in a variable.
Eg - pop( )
(3) unshift( ): Adds a data item at the beginning of an array & after adding shifts every item to right.
Eg - unshift( )
(4) shift( ): removes an item at the beginning of an array & shifts every item to the left after removing an item it also returns a value.
Eg - shift( )
(5) splice( ): Can add or remove data at any position in an array
Eg - splice( ).
Takes three args while adding an item & two args while removing.
The first argument is to specify at which index you want to remove or add an item.
The second is for removing purpose which indicates how many items you wanna remove.
Last is concerned with the item you want to add to array.
Splice also returns the removed value extra info if you don’t know the position of last elements you can use (-ve) numbers which removes the element from the end index.
Like, “-1” means last data item in an array & “-2” means second last data item in array & so on
Eg - removing an item from last index using splice( )
(6)slice( ): the slice ( ) method slices a piece of an array into new array it basically copies from an existing array to a new array
Eg - slice ( ).
(7) concat(): concat( ) method gives an new array by merging existing arrays it can be a copy of an existing array but the changes are not mirrored in the original array
Eg - concat( )
(8)indexOf( ) , lastIndexOf( ):
Gives the index of the specified content
If you wanna check if some element is present or not you can use these methods if element is not found they return “-1” as a value.
IndexOf( ) - searches from 1st index.
lastIndexOf( ) - searches from last index.
Eg - indexOf( ), lastIndexOf( ).
(9)Includes( ): you can use this method to check whether the item is present in the array or not.
Eg - includes( ).
(10)split( ) & join() (Array’s string methods)
Split(): basically does take a string, splits it by the separator you specify & turns it into array.
Eg - split( ).
The argument you pass to split is the separator.
Join(): turns an array into string.
Eg - join( ).
Separated by whitespace.
Your blog is really helpful. Keep going! ^-^
ReplyDelete