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

Ala Eddine JEBALI

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?

Matt Spinks

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

vb function returns true when false

Python: Is there a NumPy function which takes from one array when mask is true and another when mask is false?

Javascript function returns true when it should return false

If Statement Evaluates to True even when Function Returns False

Want to return true or false when finding an exact match in an array of objects?

Sinon stub - mocking a function which returns an array of objects

compareTo with objects returns a false while it is true

Function json() returns an Object with an array inside, I want an array of objects

Writing a function which compares two trees and returns true if they are equal in structure and in value and false otherwise

Describe typescript function which returns modified object with keys from array

Filtering array of objects by searching nested object properties

Can I create an object for which Array.isArray() returns true without using the Array constructor or array literal?

SwiftUI - How to default the object state back to false when another object which is selected is true inside list

instance(object, type) returns True, should it not be False?

What is the use of “true” when writing a function, which given a key and map, returns “true”

Create a function that, given an array and declared a variable, returns true or false if the variable exceeds each element of the array

Lubridate: Statement returns TRUE/FALSE on its own, but incurs "missing value where TRUE/FALSE needed" error when in function

Apps Script - Searching an array for a string and return true or false if that string is present

Set object property in an array true/false, whether the id matches with any id from another array of objects

JS, function returns undefined instead of true or false

Function returns undefined instead of true/false

Prime number function returns true instead of false

Javascript Function return true/false returns undefined

Undefined array.length returns true not false

intersectsBox returns true when expecting false

regex returns false when should return true

LINQ Query returns false when it should be true

Template returns true() when is told to return false

IF returns FALSE when it sould return TRUE