'Compare the two array of objects and render the non- matching values in react

let states =[{name:"goa", Population:1000 },{ name:"maharashtra", population : 100000 }, { name:"Kerala", Population:1000 },{ name:"Delhi", population : 100000 }, {name:"Kolkata", Population:1000 },{ name:"UP", population : 100000 }, { name:"Sikkim", Population:1000 },{name:"Ladakh", population : 100000 }];

let Covid_count=[{ name:"Kolkata", patients:20 },{ name:"UP", patients : 100 }, { name:"Sikkim", patients:10 },{ name:"Ladakh", patients : 10 }]

Display state names which does not have covid patients.



Solution 1:[1]

list of the ones with Covid:

var states =[{"name":"goa","Population":1000},{"name":"maharashtra","population":100000},{"name":"Kerala","Population":1000},{"name":"Delhi","population":100000},{"name":"Kolkata","Population":1000},{"name":"UP","population":100000},{"name":"Sikkim","Population":1000},{"name":"Ladakh","population":100000}]

var Covid_count = [{"name":"Kolkata","patients":20},{"name":"UP","patients":100},{"name":"Sikkim","patients":10},{"name":"Ladakh","patients":10}];

states.forEach((x)=>{
  if(Covid_count.find((item)=> item.name==x.name))
    console.log(x)
})

List of the ones not having covid:

var states =[{"name":"goa","Population":1000},{"name":"maharashtra","population":100000},{"name":"Kerala","Population":1000},{"name":"Delhi","population":100000},{"name":"Kolkata","Population":1000},{"name":"UP","population":100000},{"name":"Sikkim","Population":1000},{"name":"Ladakh","population":100000}]

var Covid_count = [{"name":"Kolkata","patients":20},{"name":"UP","patients":100},{"name":"Sikkim","patients":10},{"name":"Ladakh","patients":10}];

states.forEach((x)=>{
  if(!Covid_count.find((item)=> item.name==x.name))
    console.log(x)
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arsalan Ahmadi