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

134
Visualizações
moving the key of an object into its value to create an array of objects

I have an object I want to build a table out of however I need to use the value of the key as a part of the data displayed. My data looks like this:

{
    templates: {
      some_visit_1: {
        template: "A long block or rich text",
        editedAt: "timestamp",
        editedBy: "name",
      },
      some_visit_2: {
        template: "A different block of rich text",
        editedAt: "timestamp",
        editedBy: "Name",
      },
    },
  },

Ive tried using

let data = result.templates;
const templates = Object.entries(data);

But this gives me nested arrays inside with the key as one value and an object as the second. I would like to create an array of objects where each object contains the key and all values inside the initial object.

Taking this a step further I thought I could map over the new array and spread the data into an object but this just causes errors.

const templates = Object.entries(data).map((item, idx) => {
        const values = item[1];
        return {
          ...items,
          items: {
            name: item[0],
            editedAt: item[1].editedAt,
            editedBy: item[1].editedBy,
            template: item[1].template,
          },
        };
      });

Is there a method to combine the key and all values in an object into a single object?

like this:

[
   {
       {
        name: some_visit_1,
        template: "A long block or rich text",
        editedAt: "timestamp",
        editedBy: "name",
      },
      {
        name: some_visit_2,
        template: "A different block of rich text",
        editedAt: "timestamp",
        editedBy: "Name",
      }
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Is this what is expected?. I used Object.entries and map

let data = {
  templates: {
    some_visit_1: {
      template: "A long block or rich text",
      editedAt: "timestamp",
      editedBy: "name",
    },
    some_visit_2: {
      template: "A different block of rich text",
      editedAt: "timestamp",
      editedBy: "Name",
    },
  },
}
let result = Object.entries(data.templates).map(([key, value]) => {
  return {
    name: key,
    ...value,

  }
});

console.log(result);

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