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

110
Visualizações
Porblem on Getting Array Inside of Array

I have a problem on an object inside of an array and I wanted to display only that as an array.

data1

const data1 = [
  {
    "id": "01",
    "info": "fefef",
    "sub": "hieei",
    "details": {
      "data": "fruits"
    }
  },
  {
    "id": "02",
    "info": "fefef",
    "sub": "hieei",
    "details": {
      "data": "things"
    }
  }
]

expected output

const final= [
  {
    "data": "fruits"
  },
  {
    "data": "things"
  }
]

Code

 const final = data.map((data) => { ...data}) 
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Map through the array and extract its details property:

const data1 = [
  {
    "id": "01",
    "info": "fefef",
    "sub": "hieei",
    "details": {
      "data": "fruits"
    }
  },
  {
    "id": "02",
    "info": "fefef",
    "sub": "hieei",
    "details": {
      "data": "things"
    }
  }
]

const res = data1.map(e => e.details)

console.log(res)

about 4 years ago · Juan Pablo Isaza Relatório

0

map over the array and return a new object using the details property. If you don't return a new object, your new array will still carry references to the objects in the original array. So if you change a value of a property in that original array, that change will be reflected in the new array too, and you probably don't want that to happen.

const data1=[{id:"01",info:"fefef",sub:"hieei",details:{data:"fruits"}},{id:"02",info:"fefef",sub:"hieei",details:{data:"things"}}];

// Make sure you return a copy of the
// details object otherwise if you change the details
// of the original objects in the array
// the new mapped array will carry those object changes
// because the array objects will simply references to the old objects
const out = data1.map(obj => {
  return { ...obj.details };
});

console.log(out);

about 4 years ago · Juan Pablo Isaza Relatório

0

Using map and destructuring will simplify.

const data1 = [
  {
    id: "01",
    info: "fefef",
    sub: "hieei",
    details: {
      data: "fruits",
    },
  },
  {
    id: "02",
    info: "fefef",
    sub: "hieei",
    details: {
      data: "things",
    },
  },
];

const res = data1.map(({ details: { data } }) => ({ data }));

console.log(res);

// if you just need the details object

const res2 = data1.map(({ details }) => details);

console.log(res2);

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