Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

0

function foo(input) {
   return {id: input.id}; 
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda