0

I would like to know whether array of object has same property value in javascript

for the array object list1,

if name and country has same value, return true

if name same, any object country has value SG return true

if above two conditions fails, return false

var list1=[
  {id:1, name: "sen", country: "IN"},
  {id:2, name: "sen", country: "IN"}
]
const result1=checkObj(list1);

var list2=[
  {id:1, name: "sen", country: "IN"},
  {id:2, name: "sen", country: "SG"}
]
const result2=checkObj(list2);

var list3=[
  {id:1, name: "sen", country: "IN"},
  {id:2, name: "sen", country: "TH"}
]
const result3=checkObj(list3);
Expected Result:
//when passing list1
true
//when passing list2
true
//when passing list3
false
3
  • @Yogi thanks for reply, and link but need to compare in single array object itself, i got stuck Commented Apr 9, 2022 at 7:23
  • Does this answer your question? Comparing Arrays of Objects in JavaScript Commented Apr 9, 2022 at 10:30
  • @DavePile no, but need to compare in same array object itself, Commented Apr 9, 2022 at 11:23

2 Answers 2

1

Try this,

var list1=[
  {id:1, name: "sen", country: "IN"},
  {id:2, name: "sen", country: "IN"}
]
const result1=checkObj(list1);

console.log(result1);

var list2=[
  {id:1, name: "sen", country: "IN"},
  {id:2, name: "sen", country: "SG"}
]
const result2=checkObj(list2);

console.log(result2);

var list3=[
  {id:1, name: "sen", country: "IN"},
  {id:2, name: "sen", country: "TH"}
]
const result3=checkObj(list3);

console.log(result3);

function checkObj(list){
  for(i=0;i<list.length;i++)
  {  
   for(j=0;j<list.length;j++)
    {
      if(j!=i){
        if(list[i].name==list[j].name){
          if(list[i].country==list[j].country || list[i].country =='SG'){
          return true;
          }
        }
      }
    }
  }
  return false;
}

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for solution,it works, may i know how to do with es6 functions, like forEach or find, checking property in single object array
@codelearn there are many ways to solve a problem, I gave you one way. I am not your tutor. I gave you output and expect you to accept the answer
0

In the following snippet I put together an approach based on a single loop over all elements of an array. The occurrences of name/country combinations are collected in a local object o and checked at the end of the function.

const data=[[
  {id:1, name: "sen", country: "IN"},
  {id:2, name: "sen", country: "IN"}
],[
  {id:1, name: "sen", country: "IN"},
  {id:2, name: "sen", country: "SG"},
  {id:1, name: "sen", country: "DN"},
  {id:1, name: "sen", country: "EN"}
],[
  {id:1, name: "sen", country: "IN"},
  {id:2, name: "sen", country: "TH"}
]];

function dupes(arr){
  const o={}, // object o counts all occurrences
    // the sort makes sure the "SG" items come last in ar
    ar= arr.slice(0).sort((a,b)=>a.country==="SG" ? (b.country==="SG" ? 0 : 1) : -1);
  // a single loop over ar:
  ar.forEach(e=>{
   if(e.country!=="SG"){ // "normal" countries:
    let k=e.name+"|"+e.country;
    o[k]=(o[k]||0)+1
   } else {  // for country=="SG"
    for (let k in o)
     if (k.indexOf(e.name+"|")==0)
       o[k]++ // increase counts for all elements starting with <el.name>|
   }
  });
  return Object.values(o).some(n=>n>1)
   
}

data.forEach(d=>console.log(dupes(d)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.