How to avoid duplicate objects from two arrays after filter using java script

question contest:

i have two arrays ( busns and pc ) i want to check pc array -> pcid is match with busns array -> pcid after match i want to return pcname. final output is comming but i want to avoid duplicate. thanks in advance

please check my attempt in jsfiddle

const busns = [
{
 id:1,
 shopname:'New trend',
 pcid:1
},
{
 id:2,
 shopname:'Latest Beauty',
 pcid:2
},
{
 id:3,
 shopname:'Mens Style',
 pcid:1
},
{
 id:4,
 name:'Fabulook',
 pcid: 1
},
{
 id:4,
 name:'New cut',
 pcid: 2
}
]

const pc = [
       {
         pcid:1,
         pcname: 'collection1'
       },
       {
         pcid:2,
         pcname: 'collection2'
       },
       {
         pcid:3,
         pcname: 'collection3'
       },
       {
         pcid:4,
         pcname: 'collection4'
       }
    ]

My code :

busns.map(busns => {
  return pc.filter( p => {
     return busns.pcid == p.pcid
  }).map(data => {
     console.log(data.pcname)
  })
})

expected output while using console :

collection1

collection2

question comments:

    
Please see this SO question stackoverflow.com/questions/1960473/…
    
i tried that but somehow i’m missing little bit concept. tats why i posted here for help
– Narayanan S
30 mins ago
    
Try var ids = busns.map(x=>x.pcid); pc.filter(x=> ids.includes(x.pcid))
1  
@Rajesh that’s an answer not a comment.

answers:

answer1:

Something like this.

pc.filter(p => busns.map(busns => busns.pcid).includes(p.pcid)).forEach(data => console.log(data.pcname));

answer comments:

Tags:

添加评论

友情链接:蝴蝶教程