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

102
Visualizações
Convert object of objects of key value to array of object of same key value and return the after each iteration

I am trying to convert object with object inside to array of objects. I have the data as:

  "data" :{ 
    "tDetails": {
      "tName": "Limited",
      "tPay": " xyz",
    },
    "bDetails": {
      "bName": "name",
      "bid": "bid",
      "bNo": "123456",
    },
    "iDetails": {
      "iName": "iname",
      "iBranch": "ibranch",
    },
}

i want to convert it to array of objects with each iteration over the three headers(tDetails, iDetails,bDetails) as:

  const newData =  [
    {
      "id": 1,
      "title": "tDetails",
      "content": [
        {
          "id": 1,
          "key": "tName",
          "value": "Limited"
        },
        {
          "id": 2,
          "key": "tPay",
          "value": "xyz"
        }
      ]
    },
    {
      "id": 2,
      "title": "bDetails",
      "content": [
        {
          "id": 1,
          "key": "bId",
          "value": "12345"
        }
      ]
    },
    {
      "id": 3,
      "title": "iDetails",
      "content": [
        {
          "id": 1,
          "key": "iName",
          "value": "iname"
        },{
          "id":2,
          "key": "iBranch",
          "value": "ibranch"
        }
      ]
    },
   
  ]
  const NewData = () => {
    let newDetails = [];
    let newHeader = '';
    for (const header in data) {
      // header here is 'tDetails', bDetails, iDetails 
      const headerData = data[header]; 
      newDetails = Object.entries(headerData).map(([key, value]) => ({
        key,
        value,
      }));
      newHeader = header;
    }
    return [
      {
        title: newHeader,
        content: newDetails,
      },
    ];
  };

This returns me the data of only the last part(iDetails) as it is returned after the for loop.What should be changed here

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

0

Map the object's entries instead of using for..in, and in the callback, return the transformed object.

const data={tDetails:{tName:"Limited",tPay:" xyz"},bDetails:{bName:"name",bid:"bid",bNo:"123456"},iDetails:{iName:"iname",iBranch:"ibranch"}};

const newData = Object.entries(data).map(([title, obj], i) => ({
  title,
  id: i + 1,
  content: Object.entries(obj).map(([key, value], j) => ({
    id: j + 1,
    key,
    value
  }))
}));
console.log(newData);

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