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

240
Vistas
Get Names of Array of Objects without Looping

I have an array of country objects (countries) and I'm trying to get just an array of the String names, so from this example:enter image description here

Just: ['Canada', 'USA', ..] etc.

I'm trying to do this with

const getNames = (countries) => countries.map(({ Name }) => Name); 

but I can't seem to get it. Any suggestions?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Just this?

const getNames = countries.map(c => c.Name); 
about 4 years ago · Juan Pablo Isaza Denunciar

0

While topic says "without" looping, actually you can't. Map method also creates internal looping algorithm. So basically you can do something very simple and really efficient like standard for loop.

// Array of objects
const arr = [
  { id: 1, Name: "Canada", isSelected: false },
  { id: 2, Name: "USA", isSelected: false },
  { id: 3, Name: "India", isSelected: false }
];

// Function to get all params by name
const getAllParam = (arr, param, res = []) => {
  for(const e of arr) if(e[param]) res.push(e[param]);
  return res;
};

// Get all params with "Name" key
const res = getAllParam(arr, "Name");

// Test
console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You're quite close to getting it right; just remove (countries) => and you're good to go.

const getNames = countries.map(({ Name }) => Name); 

Alternatively, you can keep your code, which is a function. To get the names of the countries - coNames - call the function and pass countries as a parameter.

const getNames = (countries) => countries.map(({ Name }) => Name); 
const countries = [{id: ...., Name:.........}];
const coNames = getNames( countries );

Your code is equivalent to:

const getNames = function( countries ) {
    return countries.map(function({Name}) {
        return Name;
    });
};
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