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

214
Visualizações
How to map Object with 4 properties to Object with 3 properties?

I have two Object

one is role:

Role {
    roleId: string;
    name: string;
    description: string;
    isModerator: string;
}

role = {
    roleId:"8e8be141-130d-4e5c-82d2-0a642d4b73e1",
    name:"HR",
    description:"HR of the Company",
    isModerator:"N"
}

and 2nd is roleDetails:

RoleDetails {
    name: string;
    description: string;
    isModerator: string;
}

I want to assign roleDetails = role; so when I console.log(roleDetails) I should get:

roleDetails = {
    name:"HR"
    description:"HR of the Company"
    isModerator:"N"
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can simply assign each property individually (also note I've added , in your original object between each value):

let role = {
  roleId: "8e8be141-130d-4e5c-82d2-0a642d4b73e1",
  name: "HR",
  description: "HR of the Company",
  isModerator: "N"
};

let roleDetails = {
  name: role.name,
  description: role.description,
  isModerator: role.isModerator
};

console.log(roleDetails);

You could also do this dynamically with an array of keys:

let role = {
  roleId: "8e8be141-130d-4e5c-82d2-0a642d4b73e1",
  name: "HR",
  description: "HR of the Company",
  isModerator: "N"
};

let keys = ["name", "description", "isModerator"];

let roleDetails = Object.entries(role).reduce((a, [k, v]) => {
  if (keys.includes(k)) a[k] = v;
  return a;
}, {});

console.log(roleDetails);

And also with an array of keys to exclude:

let role = {
  roleId: "8e8be141-130d-4e5c-82d2-0a642d4b73e1",
  name: "HR",
  description: "HR of the Company",
  isModerator: "N"
};

let keysToExclude = ["roleId"];

let roleDetails = Object.entries(role).reduce((a, [k, v]) => {
  if (!keysToExclude.includes(k)) a[k] = v;
  return a;
}, {});

console.log(roleDetails);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Omit<Type, Keys> to declare type RoleDetails omitting keys

  • in this case roleId

Code:

interface Role {
  roleId: string;
  name: string;
  description: string;
  isModerator: string;
}

type RoleDetails = Omit<Role, 'roleId'>;

const role: Role = {
  roleId: '8e8be141-130d-4e5c-82d2-0a642d4b73e1',
  name: 'HR',
  description: 'HR of the Company',
  isModerator: 'N',
};

const roleDetails: RoleDetails = {
  name: role.name,
  description: role.description,
  isModerator: role.isModerator,
};
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