Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Check if object exists in another object tree in javascript

hello javascript heroes. i been suffering from this issue long time. the problem is that i'm searching inside an object using object as a key of search also using filter method. like this

let data = [
   {
    name:"Jade",
    role:"admin",
    pass:"0000"
  },
  {
    name:"Jon",
    role:"user",
    pass:"0000"
  },
  {
    name:"Adam",
    role:"user",
    pass:"0000"
  },
]

let result = data.filter((item)=>{
  return {role:"user",pass:"0000"} in item
})

i also tried

let result = data.filter((item)=>{
  return {role:"user",pass:"0000"} in {...item}
})

it always return an empty array.

please let me know if there is any way to solve this

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could read up on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes you could for exemple do and if statment of sort

about 4 years ago · Juan Pablo Isaza Report

0

You cant simply compare objects using "in" operator. You can iterate the whole object and match values. Or simply match all values in objects.

**Sample **

let data = [
  {
    name: "Jade",
    role: "admin",
    pass: "0000",
  },
  {
    name: "Jon",
    role: "user",
    pass: "0000",
  },
  {
    name: "Adam",
    role: "user",
    pass: "0000",
  },
];
//1: Matching whole object
const isEqual = (user, item) => {
  return Object.entries(user).every(([key, value]) => item[key] === value);
};

let result = data.filter((item) => {
  return isEqual({ role: "user", pass: "0000" }, item);
});
console.log(result);
// 2: Matching values
const isEqua2 = (user, item) =>
  user.role === item.role && user.pass === item.pass;

let result2 = data.filter((item) => {
  return isEqua2({ role: "user", pass: "0000" }, item);
});

console.log(result2);

about 4 years ago · Juan Pablo Isaza Report

0

do it like that :

let result = data.filter((item)=>{
 return item.role == "user" && item.pass == "0000";
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!