Go through arrays in JavaScript (Part II)
Lets talk about array and some more array methods
Hey guys , welcome to Omkar's blogs . Today we talk about some more array methods. Here are some array methods -
- lastIndexOf()
- indexOf()
- findIndex()
- reverse()
- sort()
- fill()
- find()
- includes()
lastIndexOf()
The lastIndexof() method returns the index of last occurrence of a specific element in an array.
const numList = [5, 12, 11, 22, 11, 55, 11, 24];
console.log(numList.lastIndexOf(11));
// output : 6
lastIndexOf() method return last index as 6 . Also we can pass two parameters :
- First parameter is the element to search.
- Second Parameter is the index value from where we have to start searching for element in backward way.
const numList = [5, 12, 11, 22, 11, 55, 11, 24]; console.log(numList.lastIndexOf(11 , 5)); // output : 4
indexOf()
The indexof() method returns the index of first occurrence of a specific element in an array.
const numList = [5, 12, 11, 22, 11, 55, 11, 24];
console.log(numList.indexOf(11));
// output : 2
The indexOf() method return last index as 2 . Also we can pass two parameters :
- First parameter is the element to search.
- Second Parameter is the index value from where we have to start searching for element. By default , it is 0.
Returns -1 if the element is not found in the array.const numList = [5, 12, 11, 22, 11, 55, 11, 24]; console.log(numList.lastIndexOf(11 , 5)); // output : 6
findIndex()
The findIndex() method also returns the index of first occurrence when the conditions are satisfies in function.
// defining an array of integers
const numList = [5, 12, 11, 22, 11, 55, 11, 24];
// function that returns odd number
function isEven(element) {
return element % 2 == 0;
}
// returns the index of the first odd number in the array
let firstEven = numList.findIndex(isEven);
console.log(firstEven);
// Output: 1
The findIndex() method return first index as 1 . Also we can pass two parameters :
- First parameter is callback function , which execute on each element of an array.
- Second Parameter is optional parameter . It is a object to use as
this
insidecallback
.
Returns -1 if none of the elements satisfy the function.
reverse()
The reverse() method returns the array in reverse order.
const numList = [5, 12, 11, 22, 11, 55, 11, 24];
console.log(numList.reverse());
// output : [24, 11, 55, 11, 22, 11, 12, 5]
The reverse() method create new array with reversed array elements.
sort()
The sort() method is use to sort elements of an array in ascending or descending order.
const fruits = ["Banana", "Orange", "Grapes", "Pineapple", "Apple", "Mango"];
console.log(fruits.sort());
//['Apple', 'Banana', 'Grapes', 'Mango', 'Orange', 'Pineapple']
In sort() method, we can pass a parameter as a compare function to sort in custom order.
If we want to sort a number array then, the sort() method convert number into staring and then sort then.
const numList = [5 , 1, 55, 22, 44, 100 , 1111, 154];
console.log(numList.sort());
// Output : [1, 100, 1111, 154, 22, 44, 5, 55]
To solve this problem we can do this -
// custom sorting an array of numbers
const numList = [5 , 1, 55, 22, 44, 100 , 1111, 154];
function compare(a, b){
return a - b;
}
// sort according to numbers
numList.sort(compare);
console.log(numList);
//Output : [1, 5, 22, 44, 55, 100, 154, 1111]
fill()
The fill() method returns an array by filling all elements with a specified value.
const fruits = ["Banana", "Orange", "Grapes", "Pineapple", "Apple"];
console.log(fruits.fill("Mango"));
// Output : ['Mango', 'Mango', 'Mango', 'Mango', 'Mango']
In fill() method , we can also pass 3 parameters -
- First parameter is a value to fill with.
- Second parameter is starting index.
- Third parameter is ending index.
const fruits = ["Banana", "Orange", "Grapes", "Pineapple", "Apple"];
console.log(fruits.fill("Mango" , 1 , 3));
// Output : ['Banana', 'Mango', 'Mango', 'Pineapple', 'Apple']
Since fill() is a mutator method, it returns the array itself . Not a copy of an array with change values.
find()
The find() method also returns the index of first occurrence when the conditions are satisfies in function.
// defining an array of integers
const numList = [5, 12, 11, 22, 11, 55, 11, 24];
// function that returns odd number
function isEven(element) {
return element % 2 == 0;
}
// returns the index of the first odd number in the array
let firstEven = numList.find(isEven);
console.log(firstEven);
// Output: 12
The find() method return first index as 12 . Also we can pass two parameters :
- First parameter is callback function , which execute on each element of an array.
- Second Parameter is optional parameter . It is a object to use as
this
insidecallback
.
Returns undefined if none of the elements satisfy the function.
includes()
The includes() method checks if an array contains a specified element or not.
const fruits = ["Banana", "Orange", "Grapes", "Pineapple", "Apple"];
console.log(fruits.includes("Orange" ));
// Output : true
In include() method, we can pass two parameters -
- First paramertet is value to be search.
- Second parameter is the position in the array at which to begin the search.
const fruits = ["Banana", "Orange", "Grapes", "Pineapple", "Apple"];
console.log(fruits.includes("Orange" , 2 ));
// Output : false
Thanks you for reading my blog. Some more array methods are in my next blog .
Please read and enjoy array methods.