Array in JavaScript
Description
Array is most likely a list-like object. An array is an ordered list of values. Each value is called an element specified by an index. In general JS array start with the index of 0, and the last element is at the index value equal to the value of the array’s length property minus 1. It is an special variable, and can easily hold more than one value at a time. It can’t use string as element but can use integers.
Some common operations for using ARRAY
Lets create an Array
let name = ['Alex', 'Olivia', 'Mandliv']const length = name.length
console.log(length)
//3
Access an array item by using index position
You can easily access an array’s item by its index number. But you have to remember that first index of an array is 0, and the last position will be the index value equal to the value of the array’s length property minus 1
let firstName = name[0]
//Alex let lastName = name[name.length -1]
//Mandliv
Array Methods
Array has a lot of methods to make this easier and friendly to use. Lets talk about some core methods of array in JavaScript.
Let’s add some value to Array first
Add an item to the end of an Array
.push(). This method will add an item at the end of array
let addNameLast = name.push('Rojen')
// ['Alex', 'Olivia', 'Mandliv','Rojen']
Add an item to the very beginning of an Array
.unshift(). It will add an item at the very beginning of an array
let addNameFirst = name.push('Sarif')
// ['Sarif','Alex', 'Olivia', 'Mandliv']
Let's find an item from an array
Find by index number
let name1 = name[0]// 'Alex'let name2 = name[2]// 'Mandliv'//as the very first number of an index is 0.
Find the index item in the Array
.indexOf() method .If you know a value of an item in array , you can easily get the index number of that item.
let getIndex = name.indexOf('Olivia')
//1
Let's Remove something Now
Remove an item from the end of an Array
.pop() method allows you to remove an item from the End of an array.
let removeLast = name.pop() // remove Mandliv from the array
//['Alex', 'Olivia']
Remove an item from the beginning of an Array
.shift() method allows to remove at the very beginning item from an array.
let removeFirst = name.shift()// remove Alex from the start
//['Olivia']
Slice
Slice is easier compared to Splice
//the syntax is:
array.slice([start], [end])
It slice an array and returns a new array copying to all items from start to end (actually not including the end). For Example:
let slicingName = name.slice(0,2) // removes Mandliv from the array
//['Alex','Olivia']let slicingName = name.slice(-2)// ['Olivia','Mandliv']
// it will start count from the End and it starts with 1 not 0.
Some other Methods
.concat()
The method array.concat creates a new array that includes values from other arrays and additional items. The sytext is:
array.concat(arg1, arg2...)
The result of new array will contain items from array
, then arg1
, and then arg2
. If the argument
is a single item then it copies itself . If the argument
is an another array
then it copies all the items from the argument
let concatName = name.concat('okay')
// ['Alex', 'Olivia', 'Mandliv','Okay']let concatNameArray = name.concat(['Hi', 'Hello'],['Bye','Tata'])
// ['Alex', 'Olivia', 'Mandliv','Hi','Hello','Bye','Tata']
.filter()
The .filter()
method simply creates a new array by taking elements those
can pass the test provided by the given function
let filterName = name.filter( singleName => singleName.length <= 6)
//['Alex', 'Olivia']
.every()
The .every()
method always returns Boolean value. If it finds any item which can't pass the test given by the function it directly returns false
let name = ['Alex', 'Olivia', 'Mandliv']
const canPass = (canPass) => canPass.length < 6;console.log(name.every(canPass))
// false as Alex has a toltol of 4 lengthconst nowPass= (canPass) => canPass.length < 4;
//true as all the name have more than 4 length