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?
@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.
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.
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.)
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With