Useful Array searching methods in Javascript

includes()  Method

In JavaScript, includes() method determines whether a string contains the given characters within it or not. This method returns true if the string contains the characters, otherwise, it returns false.

        
const array = [50, 22, 31, 20, 9];
const check = array.includes(20);
const check1 = array.includes(33);

console.log(check)
console.log(check1)

//Output:
true
false
        
    

find()  Method

The find() method returns the first value in an array that matches the conditions of a function. If there is no match, the method returns undefined. Just like the filter method, it takes a callback as an argument and returns the first element that meets the callback condition. Let’s understand by example.

        
const array = [30, 44, 3, 90, 25];

const findarray = array.find(element => element > 50);
console.log(findarray);

const findarray1 = array.find(element => element > 20);
console.log(findarray1)

//Output :
90
30
        
    

From the above example, find() method will return the element that has a value greater than 50 & 20 respectively.

        
const person = [
  { name: 'john',  category: 'singer'},
  { name: 'cart',  category: 'carpenter'},
  { name: 'harry', category: 'painter'},
  { name: 'justin',  category: 'fisher'},
  { name: 'salina',  category: 'singer'}
];

const find = person.find(element => element.category === 'singer');

console.log(find);

//Output : { name: 'john', category: 'singer' }        
    

From the above example find the method finds an element that has a category as a singer. So first matched element is a return.

indexof()  Method

The indexOf() method returns the first index at which a given element can be found in an array. It returns -1 if the element does not exist in the array.

        
const array = [70, 44, 35, 2, 55];
const index = array.indexOf(55);
console.log(index)

const index1 = array.indexOf(35);
console.log(index1)

const index2 = array.indexOf(95);
console.log(index2)

//Output :
4
2
-1
        
    

As we see in the above example, the index of number should return. If a number is not found in the list then -1 will be returned.

We can also check from the object. Let’s see in the example below

        
const array = [
    {name:'john',color:'white'},
    {name:'carter',color:'black'},
    {name:'shaw',color:'blue'},
    ];

const index = array.findIndex((element)=>element.color==='blue');
console.log(index);

//Output : 2
        
    

findindex()  Method

This method is very similar to find. It also accepts a callback function to apply your search criteria. However, it returns the index at which the element is located within the array instead of its element. When the element is not found,findIndex returns -1.

        
const array = [20, 44, 35, 2, 55];
const index = array.findIndex((element)=>element>30);

console.log(index);

const index1 = array.findIndex((element)=>element>0);

console.log(index1);

//Output :
1
0
        
    
        
const person = [
  { name: 'john',  category: 'singer'},
  { name: 'cart',  category: 'carpenter'},
  { name: 'harry', category: 'painter'},
  { name: 'justin',  category: 'fisher'},
  { name: 'salina',  category: 'singer'}
];

const find = person.findIndex(element => element.category === 'fisher');

console.log(find);

//Output : 3
        
    

some()  Method

This method checks whether at least one of the array items matches the search criteria determined in the callback function. If it does, the method will return true, otherwise, it will return false.

        
const array = [20, 44, 35, 2, 55];

console.log(array.some((element)=>element>30));
console.log(array.some((element)=>element>100));

//Output :
true
false
        
    
        
const person = ["Micheal", "Seth", "Brayan", "Samoa", "Rollins"];
const name = person.some(CheckName => {
    return CheckName[0] === "S";
});

console.log("All names Starts with J : " , name);

//Output : All names Starts with J :  true
        
    

filter() Method

filter() that takes a condition as a parameter and returns a new array of only those elements that fulfill the specified condition. This method checks each element in an array to see if it meets a condition. It returns a new array with the elements that meet the condition.

        
const person = ["John", "Rollins", "Smith", "Roddy", "Randy"];
const FilteredNames = person.filter(name => {
    return name[0] === "R";
});

console.log("Employee name : " , FilteredNames);

//Output : Employee name :  [ 'Rollins', 'Roddy', 'Randy' ]
        
    
Posted in

Ankit Patel

Leave a Comment

Your email address will not be published.

Scroll to Top