1

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
codelearn
  • 59
  • 6
  • @Yogi thanks for reply, and link but need to compare in single array object itself, i got stuck – codelearn Apr 09 '22 at 07:23
  • Does this answer your question? [Comparing Arrays of Objects in JavaScript](https://stackoverflow.com/questions/27030/comparing-arrays-of-objects-in-javascript) – Dave Pile Apr 09 '22 at 10:30
  • @DavePile no, but need to compare in same array object itself, – codelearn Apr 09 '22 at 11:23

2 Answers2

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;
}
Shoyeb Sheikh
  • 2,659
  • 2
  • 10
  • 19
  • 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 Apr 09 '22 at 10:07
  • @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 – Shoyeb Sheikh Apr 09 '22 at 19:02
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)))
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43