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

105
Views
How to check two array of objects property using javascript

I would like to know how to return true or false based on two array objects property using javascript

If arrobj1 and arrobj2 value and country same return true

I have tried below, may i know better way to do using javascript

Tried

for(var elem of arrobj1){
 const result = arrobj2.filter(obj => obj.value === elem.value && obj.country === elem.country);
 if(result.length > 0){
 return true;
}
}

var arrobj1 =[
  {id:1, name: "one", value:"sales", country:"MY"},
  {id:2, name: "two", value:"finance", country:"PH"},
  {id:3, name: "three", value:"digital", country:"SL"}
]

var arrobj2 =[
 {id:4, name: "four", value:"digital", country:"SL"}
]

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

0

Rearrange your code to start with the arrays then the sorting function

arrobj2 should be:

 var arrobj2 =[
 {id:4, name: "four", value:"digital", country:"SL"}
]

instead of:

var arrobj2 =[{
 {id:4, name: "four", value:"digital", country:"SL"}
}]

also try console.log('true') instead if return true. You'll notice something that would help you later

about 4 years ago · Juan Pablo Isaza Report

0

  • Your code works perfectly. To use the return keyword, you have to wrap your code in a function and call it when needed.

Try this

var arrobj1 = [
    {id:1, name: "one", value:"sales", country:"MY"},
    {id:2, name: "two", value:"finance", country:"PH"},
    {id:3, name: "three", value:"digital", country:"SL"}
]

var arrobj2 = [
    {id:4, name: "four", value:"digital", country:"SL"}
];

function exist(arrobj1, arrobj2){
    for(var elem of arrobj1){
        const result = arrobj2.filter(obj => obj.value === elem.value && obj.country === elem.country);
        if(result.length > 0){
            return true;
        }
    }
    return false;
}
console.log(exist(arrobj1, arrobj2));

about 4 years ago · Juan Pablo Isaza Report

0

You can use Array#some as follows:

const 
arrobj1 =[ {id:1, name: "one", value:"sales", country:"MY"}, {id:2, name: "two", value:"finance", country:"PH"}, {id:3, name: "three", value:"digital", country:"SL"} ], 
arrobj2 =[ {id:4, name: "four", value:"digital", country:"SL"} ],

found = arrobj2.some(
    ({value:v,country:c}) => 
    arrobj1.map(
        ({value,country}) => `${value}|${country}`
    ).includes(`${v}|${c}`)
);

console.log( found );

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!