Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

316
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda