Open In App

How to Search Character in List of Array Object Inside an Array Object in JavaScript ?

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Searching for a character in a list of array objects inside an array object involves inspecting each array object, and then examining each array within for the presence of the specified character. The goal is to determine if the character exists within any of the nested arrays. There are several ways to search for characters in a list of array objects inside an array of objects which are as follows:

Using forEach() loop

In this approach, we Iterate through each element in an array or object by using the forEach() loop . To search for a character, for instance, within an array object, nested forEach() loops must examine every level of the structure.

Syntax:

array.forEach(callback(element, index, arr), thisValue);

Example: The myFunction iterates through arrayOfObjects, searching for the specified character in nested arrays using the forEach() loop.

Javascript




function myFunction(arrayOfObjects, char) {
  let found = false;
 
  arrayOfObjects.forEach((obj) => {
    obj.charLists.forEach((charList) => {
      if (charList.includes(char)) {
        found = true;
      }
    });
  });
 
  return found;
}
 
const arrayOfObjects = [
  { charLists: ["HTML", "CSS", "JavaScript"] },
  { charLists: ["React", "Redux", "Routes"] },
  { charLists: ["10", "20", "30"] },
];
 
const result1 = myFunction(arrayOfObjects, "React");
console.log(result1);
const result2 = myFunction(arrayOfObjects, "PHP");
console.log(result2);


Output

true
false

Using Array.some() and Array.includes()

In this approach, we use Array.some() to efficiently search for a specific character in a list of array objects within an array object. Employ Array.includes() within the some() callback to determine if the target character exists, returning a boolean result.

Syntax:

// Array.some()
arr.some(callback(element,index,array),thisArg);
//Array.includes()
array.includes(searchElement, start);

Example: The myFunction employs Array.some() to iterate through arrayOfObjects and check if the target character exists in any charLists.

Javascript




function myFunction(arrayOfObjects, char) {
  return arrayOfObjects.some((obj) =>
    obj.charLists.some((charList) => charList.includes(char))
  );
}
 
const arrayOfObjects = [
  { charLists: ["HTML", "CSS", "JavaScripat"] },
  { charLists: ["React", "Redux", "Routes"] },
  { charLists: ["10", "20", "30"] },
];
 
const result1 = myFunction(arrayOfObjects, "React");
console.log(result1);
 
const result2 = myFunction(arrayOfObjects, "PHP");
console.log(result2);
 
const result3 = myFunction(arrayOfObjects, "20");
console.log(result3);


Output

true
false
true

Using Array.find() method

In this approach we are using Array.find() to iterate over the array and objects. The Array. find() method in JavaScript is a handy tool for searching through an array and retrieving the first element that satisfies a specified condition then we use some() to search nested arrays for the given character. It returns a boolean result.

Syntax:

arr.find(callback(element,index,array),thisArg);

Example: In this example, The myFunction takes an array of objects and a character as parameters. It uses Array.find() and Array.some() to search for the character within nested arrays. Returns true if found, otherwise false

Javascript




function myFunction(arrayOfObjects, char) {
  return (
    arrayOfObjects.find((obj) =>
      obj.charLists.some((charList) => charList.includes(char))
    ) !== undefined
  );
}
 
const arrayOfObjects = [
  { charLists: ["HTML", "CSS", "JavaScript"] },
  { charLists: ["React", "Redux", "Routes"] },
  { charLists: ["10", "20", "30"] },
];
 
const result1 = myFunction(arrayOfObjects, "React");
console.log(result1);
 
const result2 = myFunction(arrayOfObjects, "PHP");
console.log(result2);
 
const result3 = myFunction(arrayOfObjects, "20");
console.log(result3);


Output

true
false
true

Using reduce() method

Using the Array.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator. Array.reduce() method is used to search for a character in a list of array objects within an array object. and return a boolean value by using Array.includes().

Syntax:

arr.reduce(callback(element,index,array),thisArg);

Example: The myFunction uses array.reduce() to iterate through arrayOfObjects and check if the target character exists in any charLists.

Javascript




function myFunction(arrayOfObjects, char) {
  return arrayOfObjects.reduce(
    (found, obj) =>
      found || obj.charLists.some((charList) => charList.includes(char)),
    false
  );
}
 
const arrayOfObjects = [
  { charLists: ["HTML", "CSS", "JavaScript"] },
  { charLists: ["React", "Redux", "Routes"] },
  { charLists: ["10", "20", "30"] },
];
 
const result1 = myFunction(arrayOfObjects, "React");
console.log(result1);
 
const result2 = myFunction(arrayOfObjects, "PHP");
console.log(result2);
 
const result3 = myFunction(arrayOfObjects, "20");
console.log(result3);


Output

true
false
true


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads