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

70
Vistas
How to keep a few properties in each returned object from a fetch call?

I got a fetch call that return 100+ objects in an array. Each object got about 10 properties. I want to keep only 3 properties.

Example of returned JSON:

[
  {
    id: 'base1-1',
    name: 'object1',
    supertype: 'Pokémon',
    subtypes: ['Stage 2'],
    level: '42',
    hp: '80',
  },
.
.
//Another 100 objects like that
.
.
  {
    id: 'base1-100',
    name: 'object100',
    supertype: 'Pokémon',
    subtypes: ['Stage 4'],
    level: '42',
    hp: '80',
  },
];

I am using React.useState to set this array of objects into an array (cards) buy I don't want to save all these props inside each object. I only want name, and id. How to discard the rest?

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

0

I'd probably use map, using destructuring to pick out the 2-3 properties I wanted; and shorthand property notation when creating replacement objects:

const result = objects.map(({id, name}) => ({id, name}));
// Destructuring −−−−−−−−−−−^^^^^^^^^^      ^^^^^^^^^^−−− object literal with shorthand properties

(You need () around the object literal when returning it from a concise-form arrow function because otherwise the { looks like the beginning of a function body block.)

That code is roughly equivalent to:

const result = objects.map(obj => {
    const id = obj.id;
    const name = obj.name;
    return {
        id: id,
        name: name
    };
});

Live Example:

const objects = [
  {
    id: 'base1-1',
    name: 'object1',
    supertype: 'Pokémon',
    subtypes: ['Stage 2'],
    level: '42',
    hp: '80',
  },
  // ...
  {
    id: 'base1-100',
    name: 'object100',
    supertype: 'Pokémon',
    subtypes: ['Stage 4'],
    level: '42',
    hp: '80',
  },
];
const result = objects.map(({id, name}) => ({id, name}));
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Array.prototype.map like this:

const mappedArray = originalArray.map(({firstProp, secondProp, thirdPropd}) => ({
  firstProp,
  secondProp,
  thirdProp,
}));
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