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

314
Views
How to filter an array based on property value and return array of objects

I have this array of objects with the name and isPinned keys. I would like to store only the country name in a new array based on what isPinned is. For example, if Australia and China have isPinned as true, then the new array would be:

const countriesFiltered = ["Australia", "China"]

const countries = [
    { name: "Australia", isPinned: true },
    { name: "China", isPinned: true },
    { name: "India", isPinned: false },
  ];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You would .filter by isPinned and then .map by name

const namesOfPinnedCountries = countries
    .filter(it => it.isPinned)
    .map(it => it.name)
about 4 years ago · Juan Pablo Isaza Report

0

You can use flatMap:

const countries = [
    { name: "Australia", isPinned: true },
    { name: "China", isPinned: true },
    { name: "India", isPinned: false },
];
const countriesFiltered = countries
    .flatMap(i => i.isPinned ? i.name : []);
    
console.log(countriesFiltered);

about 4 years ago · Juan Pablo Isaza Report

0

.filter() and .map() is the classical solution, but you can also do it with .reduce():

const countries = [
{ name: "Australia", isPinned: true },
{ name: "China", isPinned: true },
{ name: "India", isPinned: false },
  ];

const res=countries.reduce((a,c)=>
  c.isPinned?[...a,c.name]:a,[]);

console.log(res);

about 4 years ago · Juan Pablo Isaza 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!