Methods to search elements in arrays

Hi, in this post I’m going to show you how to use different techniques than can help you to search elements in arrays or know if some elements are in the array.

Maybe you are confused and how use the different methods like .map(), .filter(), .includes(), etc. but check this post 🙂

array.includes()

If we want to know if some element exists in our array then this method is the indicated, this method will return us “true” if the valued passed exist in our array or “false” if not exist in our array the syntax of this method is:

array.includes(element, start_position);

Check the next example:

// My todo list in an array
let todoList = ["cook", "read book", "laundry"]; 

// Function searchElement
function searchElement(searchedValue) {
    
   const result =  todoList.includes(searchedValue);
    
    // returning the function
    return result;
}

searchElement("read book");

Result:

array.some()

By definition the .some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument method, the answer will be returned as “true or false”.

The syntax for this method is:

array.some(function(currentValue, index, array), thisValue)

Let’s see the example:

// My todo list in an array
let todoList = ["cook", "read book", "laundry"]; 

// Function searchElement
function searchElement(searchedValue) {
    // This method returns true even if one of the elements of the array satisfies the condition
   const result =  todoList.some((element) => element === searchedValue);
    
    // returning the function
    return result;
}

searchElement("read book");

Result:

Nice, but what about if instead of a regular array I have an array of objects? like [{}, {}, {}]

Well, then .some() method is the indicated to solve this if we want the answer “true or false“:

// My todo list in an array of objects
let todoList = [
    {
        todo: "cook"
    },
    {
        todo: "read book"
    },
    {
        todo: "laundry"
    }
]; 

// Function searchElement
function searchElement(searchedValue) {
    // This method returns true even if one of the elements of the array satisfies the condition
   const result =  todoList.some((element) => element.todo === searchedValue);
    
    // returning the function
    return result;
}

searchElement("read book");

Result:

array.find()

What about if instead of the answer “true or false” I want the information (value) of that element?

Well, for this we can use the .find() method that returns the value of the first element in an array that pass a test (provided as a function). If the provided function return true then .find() will return the value of that element in the array.

// My todo list in an array
let todoList = ["cook", "read book", "laundry"]; 

// Function searchElement
function searchElement(searchedValue) {
    
   const result = todoList.find((element) => element === searchedValue);
    
    // returning the function
    return result;
}

searchElement("read book");

Result:

.find() method will work for an array of objects look:

// My todo list in an array of objects
let todoList = [
    {
        todo: "cook"
    },
    {
        todo: "read book"
    },
    {
        todo: "laundry"
    }
]; 

// Function searchElement
function searchElement(searchedValue) {
    
   const result = todoList.find((element) => element.todo === searchedValue);
    
    // returning the function
    return result;
}

searchElement("read book");

Result:

array.filter()

This method is similar to .find() method but the main difference is that .filter() will return all the matching elements in a new array!.

// My todo list in an array
let todoList = ["cook", "read book", "laundry", "read book"]; 

// Function searchElement
function searchElement(searchedValue) {
    
   const result = todoList.filter((element) => element === searchedValue);
    
    // returning the function
    return result;
}

searchElement("read book");

Result:

.filter() method will work for an array of objects look:

// My todo list in an array of objects
let todoList = [
    {
        todo: "cook"
    },
    {
        todo: "read book"
    },
    {
        todo: "laundry"
    },
    {
        todo: "read book"
    }
]; 

// Function searchElement
function searchElement(searchedValue) {
    
   const result = todoList.filter((element) => element.todo === searchedValue);
    
    // returning the function
    return result;
}

searchElement("read book");

Result:

By Cristina Rojas.