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

119
Vistas
How to cast objects?

I have this object:

{
  "id": "Test",
  "firstname": "Test",
  "lastname": "Test",
  "age": 83
}

but i want to return object only with this value:

{
    "id": "Test"
}

how to cast first object to another one with using TS/NodeJS?

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

0

You could write a function like this that uses a property accessor to only return the id of the object:

let data = { id: 'Test', firstname: 'Test', lastname: 'Test', age: 83, };

function trimToId(data) {
  return { id: data.id };
}

console.log(trimToId(data))

If you wanted to make this more concise you could use an arrow function with destructuring:

let data = { id: 'Test', firstname: 'Test', lastname: 'Test', age: 83 };

const trimToId = ({ id }) => ({ id });

console.log(trimToId(data));

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think what you are looking for is a Utility Type called Partial. You can find the documentation here.

interface FirstObject {
  id: string;
  firstname: string;
  lastname: string;
  age: number;
}

const firstObject: FirstObject = {
  "id": "Test",
  "firstname": "Test",
  "lastname": "Test",
  "age": 83
};

const secondObject: Partial<FirstObject> = {
  "id": "Test"
};

about 4 years ago · Juan Pablo Isaza Denunciar

0

function foo(input) {
   return {id: input.id}; 
}
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