Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
Filter duplicate records and push into array with additional condition using js

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 );
almost 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You should try this in your code!

just put condition in the result statement with arrow function and you are good to go.

    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,
  },
];

const result = arr.filter((data) => data.temp < 40 && data.state !== data.city);
const data = result.map((x) => x.city);
console.log(data);
almost 4 years ago · Santiago Trujillo Report

0

let arr = [
  {
    city: "chennai",
    state: "tamilnadu",
    temp: 44,
  },
  {
    city: "coimbator",
    state: "tamilnadu",
    temp: 39,
  },
  {
    city: "mumbai",
    state: "maharashtra",
    temp: 32,
  },
  {
    city: "mumbai",
    state: "maharashtra",
    temp: 32,
  },
  {
    city: "delhi",
    state: "delhi",
    temp: 24,
  },
  {
    city: "kolkata",
    state: "west bengal",
    temp: 28,
  },
];
const result = [
...new Set(
arr.filter((data) => data.temp < 40 && data.state !== data.city).map((data) => data.city)
)
];
console.log(result);

almost 4 years ago · Santiago Trujillo Report

0

This checks for uniqueness of city and state values and also handles your additional conditions. It will not add values which have the same city and the same state value as any other object in the array.

const arr = [
  {
    city: "chennai",
    state: "tamilnadu",
    temp: 44,
  },
  {
    city: "coimbator",
    state: "tamilnadu",
    temp: 39,
  },
  {
    city: "mumbai",
    state: "maharashtra",
    temp: 32,
  },
  {
    city: "mumbai",
    state: "maharashtra",
    temp: 32,
  },
  {
    city: "delhi",
    state: "delhi",
    temp: 24,
  },
  {
    city: "kolkata",
    state: "west bengal",
    temp: 28,
  }
];

const result = arr.reduce((all, cur) => { 
  if (
    all.findIndex((c) => c.city === cur.city && c.state === cur.state) < 0 &&
    cur.state !== cur.city && 
    cur.temp < 40
  ) {
    all.push(cur);
  }

  return all;
}, []);

console.log(result);

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!