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

74
Visualizações
Destructuring first object of array in JavaScript

Let's say we have the object below:

const page = {
        name: 'Page 1',
        page_category: [
          {
            postId: 1,
            catId: 1,
            category: {
              id: 1,
              name: 'category 1'
            }
          },
          {
            postId: 3,
            catId: 2,
            category: {
              id: 2,
              name: 'category 2'
            }
          },
        ]
      }

To get the first object in the page_category array, in a destructuring manner, We would do this:

const { page_category: [first] } = page

But what would it be if we wanted to get the first object's category field?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can destructure the object within the array destructuring:

const { page_category: [{category}] } = page;

const page = {
  name: 'Page 1',
  page_category: [{
      postId: 1,
      catId: 1,
      category: {
        id: 1,
        name: 'category 1'
      }
    },
    {
      postId: 3,
      catId: 2,
      category: {
        id: 2,
        name: 'category 2'
      }
    },
  ]
};

const { page_category: [{category}] } = page;
console.log(category);

The destructuring assignment syntax is analogous to the syntax of creating the object literal, the below formatting shows how they have the same shape. You can use this idea to assist with your destructuring patterns:

const { 
  page_category: [
    {
      category
    }
  ]
} = page;
about 4 years ago · Juan Pablo Isaza Relatório

0

To destructure the first object's category field.

const {page_category: [ {category} ]} = page;

console.log("category ==>", category);

This way you will find the first category object.

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