Your wait is over. I have come back with part - 2 of Javascript array methods.
So, this is part - 2. And we have already discussed 10 Javascript methods in the previous blog (Part - 1). In case you missed it, check it out here.
Let's start!!
1. sort()
Sorts the elements of the array alphabetically. (sort - arranging the elements in either ascending or descending order).
In case it is an array of strings you will find that it works fine, but when you use it for the array of numbers you will notice that it is different from what you were expecting.
Actually, what happens is the array elements (numbers) are first converted to strings and then sorted.
for e.g. '2' > '100'
by default, elements are sorted in ascending order.
Syntax:
Array.sort()
const str = ['one', 'two', 'three', 'four']
str.sort();
console.log(str); //['four', 'one', 'three', 'two']
const arr = [100,21,3,120,1000,1];
arr.sort();
console.log(arr); //[1, 100, 1000, 120, 21, 3]
But if you want to sort it by numbers to get normal order, then follow the below code.
for ascending order
arr.sort(function(a,b){
return a-b;
})
console.log(arr); // [1, 3, 21, 100, 120, 1000]
for descending order
arr.sort(function(a,b){
return b-a;
})
console.log(arr); // [1000, 120, 100, 21, 3, 1]
For more details, check it out Mdn docs
2. includes()
Checks if an array contains the given value. Returns true if it contains else return false.
Syntax:
Array.includes(searchValue,start);
searchValue is the value to be searched.
start tells from which index to start searching. It is optional and Its default value is 0.
const arr = [10,20,30,40];
let result = arr.includes(20);
console.log(result); //true
result = arr.includes(20,2);
console.log(result); //false
3. indexOf()
Returns the index of the first occurrence of a value in the array. In case the value is not found it returns -1.
Syntax :
Array.indexOf(searchValue,start);
searchValue is the value to be searched.
start tells from which index to start searching. It is optional and Its default value is 0.
const arr = [10,20,30,20,40];
let result = arr.indexOf(20);
console.log(result); // 1
result = arr.indexOf(20,2);
console.log(result); // 3
result = arr.indexOf(10,1);
console.log(result); // -1
4. lastIndexOf()
Returns the index of the last occurrence of a value in the array. In case the value is not found it returns -1.
The array is searched from right to left.
Syntax:
Array.lastIndexOf(searchValue, start);
const arr = [10,20,30,20,40];
let result = arr.lastIndexOf(20);
console.log(result); // 3
result = arr.lastIndexOf(20,2);
console.log(result); // 1
result = arr.lastIndexOf(40,3);
console.log(result); // -1
5. find()
Returns the first element which satisfies the given condition implemented in the function.
If there is no such element in the array, it returns undefined.
const arr = [10,20,30,40];
let result = arr.find( ele => ele > 25);
console.log(result); //30
result = arr.find( ele => ele < 10);
console.log(result); //undefined
I have used the Arrow function here. If you are not aware of the Arrow function, get familiar with this here
6. findIndex()
Finds the index of the first element which satisfies the given condition implemented in the function.
If there is no such element in the array, it returns -1.
const arr = [10,20,30,40];
let result = arr.findIndex( ele => ele > 25);
console.log(result); //2
result = arr.findIndex( ele => ele < 10);
console.log(result); //-1
7. every()
Checks if all the array elements pass the test implemented in the function.
If all the elements pass the test then it returns true. But if any one of them fails the test, it returns false.
const count = [12,45,65,23,47,89];
let result = count.every((ele)=> ele > 10);
console.log(result); //true
result = count.every((ele)=> ele > 20);
console.log(result); //false
8. some()
Checks if at least one element in the array passes the test implemented in the function.
If any one element of the array passes the test then it returns true. But if no element passes the test, it returns false.
const count = [12,45,65,23,47,89];
let result = count.some((ele)=> ele > 50);
console.log(result); //true
result = count.some((ele)=> ele < 10);
console.log(result); //false
Hey, you have made it to the end. ๐
Thanks for reading this blog.
If you found it useful, give it a thumbs-up. ๐
Don't forget to check out Part 3, it includes more important javascript array methods.