Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function which returns true or false when searching for an object in an array of objects?

I'm looking for a good way to check if an object exist in an array of objects. The intended result is true when all keys/values are present in the same object in that array.

The answers I found by browsing stackoverflow like Find object by id in an array of JavaScript objects which is using jQuery.grep or Find a value in an array of objects in Javascript return the found object. What I'm looking for is a boolean result (not the found object).

I know that I can loop for all array elements and then compare each value....etc

But what I mean is if there is a way to use JS methods like this:

var listOfObjecs = [  	{id: 1, name: "Name 1", score: 11},  	{id: 2, name: "Name 2", score: 22},  	{id: 3, name: "Name 3", score: 33},  	{id: 4, name: "Name 4", score: 44},  	{id: 5, name: "Name 5", score: 55},  ];    var isObjectExist = function(search){    return listOfObjecs.filter(function(obj){       if(obj.id===search.id && obj.name===search.name && obj.score===search.score){            return true;       }       return false;    });  }    console.log( isObjectExist({id: 3, name: "Name 3", score: 33}) );  //outputs the found object [{id: 3, name: "Name 3", score: 33}]    console.log( isObjectExist({id: 9, name: "Name 3", score: 33}) );  //outputs an empty object []

This is because:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

If there is no native JavaScript method (returning true or false) how can update the isObjectExist() function to return true or false?

like image 801
Ala Eddine JEBALI Avatar asked Jan 30 '17 19:01

Ala Eddine JEBALI


People also ask

Does find return true or false?

@zero298 The find() method returns a value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned. So no boolean result.

Which of the following function of array object returns true if at least one element in this array satisfies the provided testing function?

some() The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false.

How do you search an array of objects for a specific value?

If you need the index of the found element in the array, use findIndex() . If you need to find the index of a value, use Array.prototype.indexOf() . (It's similar to findIndex() , but checks each element for equality with the value instead of using a testing function.)


2 Answers

The Array.prototype.some method:

The some() method checks if any of the elements in an array pass a test (provided as a function). The some() method executes the function once for each element present in the array: If it finds an array element where the function returns a true value, some() returns true (and does not check the remaining values)

like image 58
Matt Spinks Avatar answered Sep 25 '22 01:09

Matt Spinks


I'm sharing an example based on comments above, it may help others:

  const listOfObjecs = [      { id: 1, name: "Name 1", score: 11 },      { id: 3, name: "Name 3", score: 33 },      { id: 2, name: "Name 2", score: 22 },      { id: 3, name: "Name 3", score: 33 },      { id: 4, name: "Name 4", score: 44 },      { id: 5, name: "Name 5", score: 55 },      { id: 3, name: "Name 3", score: 33 },    ];    const search1 = { id: 3, name: "Name 3", score: 33 };    const search2 = { id: 9, name: "Name 3", score: 33 };      // Using Array.prototype.some()    const resSomeSearch1 = listOfObjecs.some(item => JSON.stringify(item) === JSON.stringify(search1));    console.log(`resSome(search1): ${resSomeSearch1}`); // outputs: true    const resSomeSearch2 = listOfObjecs.some(item => JSON.stringify(item) === JSON.stringify(search2));    console.log(`resSome(search2): ${resSomeSearch2}`); // outputs: false      // Using Array.prototype.filter()    const resFilterSearch1 = !!listOfObjecs.filter(item => JSON.stringify(item) === JSON.stringify(search1)).length;    console.log(`resFilter(search1): ${resFilterSearch1}`); // outputs: true    const resFilterSearch2 = !!listOfObjecs.filter(item => JSON.stringify(item) === JSON.stringify(search2)).length;    console.log(`resFilter(search2): ${resFilterSearch2}`); // outputs: false      // Using Array.prototype.find()    const resFindSearch1 = !!listOfObjecs.find(item => JSON.stringify(item) === JSON.stringify(search1));    console.log(`resFind(search1): ${resFindSearch1}`); // outputs: true    const resFindSearch2 = !!listOfObjecs.find(item => JSON.stringify(item) === JSON.stringify(search2));    console.log(`resFind(search2): ${resFindSearch2}`); // outputs: false
like image 24
Ala Eddine JEBALI Avatar answered Sep 25 '22 01:09

Ala Eddine JEBALI