I wan to filter the list of objects where city and state are not same, and then create a list of cities out of the filtered objects where temperature is less than 40. but condition is both sate and city should not be same
let arr = [
{
city:"chennai",
state: "tamilnadu",
temp: 44
},
{
city:"coimbator",
state: "tamilnadu",
temp: 39
},
{
city:"mumbai",
state: "maharashtra",
temp: 32
},
{
city:"delhi",
state: "delhi",
temp: 24
},
{
city:"kolkata",
state: "west bengal",
temp: 28
}
];
Javascript code:
const uniqueStateCity = [];
const unique = arr.filter(element => {
const isDuplicate = uniqueStateCity.includes(element.city);
if (!isDuplicate) {
if(element.temp < 40)
{
uniqueStateCity.push(element.city );
return true;
}
}
return false;
});
console.log(unique );