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

145
Visualizações
storing data by removing nested key by using node js / javascript

let json=[
 {
  metadata: { tags: [] },
  sys: {
    space: { sys: [Object] },
    id: '4gSSbjCFEorYXqrgDIP2FA',
    type: 'Entry',
  },
  fields: {
    richTextEditor: { 'fn-US': [Object] },
    short: { 'as-ds': 'hello default value' },
    shortUrl: { 'en-US': 'www.google.com' },
    shortSlugOption: { 'zh-cht': 'hello-default-value' },
    mediaFileAssetLink: { 'ur-pk': [Array] },
    mediaFileAssetGalary: { 'en-US': [Array] },
    singleMediaImage: { 'cy-az-az': [Object] },
    booleanField: { 'en-US': true },
    locationField: { 'zh-mo': [Object] },
    dateNTime: { 'sv': '2022-02-01T00:00+05:30' }
  }
}
];


        json.map((data) => {
          console.log(data.fields);
         /* fs.writeFile(
            "./data.json",
            JSON.stringify(data.fields, null, 4),
            (err) => {
              if (err) throw console.log(err.messages);
            }
          ); */
        });

I got this object and I wrote map function to fetch all the fields which I got from the output.

And I am trying to store in JSON file but I don't want to store data in this format

Expected format

    richTextEditor:  [Object] ,
    short: 'hello default value' ,
    shortUrl: 'www.google.com' ,
    shortSlugOption: 'hello-default-value' ,
    mediaFileAssetLink:  [Array] ,
    mediaFileAssetGalary: [Array] ,
    singleMediaImage: [Object] ,
    booleanField: true ,
    locationField: [Object] ,
    dateNTime: '2022-02-01T00:00+05:30' 

How can I remove all the nested 'fn-us','as-ds' n all and store the value in expected format

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

0

You can e.g. use Object.entries and Object.fromEntries() in combination with .map() to return the new structure:

let json=[ { metadata: { tags: [] }, sys: { space: { sys: [Object] }, id: '4gSSbjCFEorYXqrgDIP2FA', type: 'Entry', }, fields: { richTextEditor: { 'fn-US': [Object] }, short: { 'as-ds': 'hello default value' }, shortUrl: { 'en-US': 'www.google.com' }, shortSlugOption: { 'zh-cht': 'hello-default-value' }, mediaFileAssetLink: { 'ur-pk': [Array] }, mediaFileAssetGalary: { 'en-US': [Array] }, singleMediaImage: { 'cy-az-az': [Object] }, booleanField: { 'en-US': true }, locationField: { 'zh-mo': [Object] }, dateNTime: { 'sv': '2022-02-01T00:00+05:30' } } } ];
  
const res = json.map((data) => {
    const fields = Object.fromEntries(Object.entries(data.fields).map(el=>(el[1]=Object.values(el[1])[0], el)))
    return {...data, fields }
            /* fs.writeFile(
               "./data.json",
               JSON.stringify(data.fields, null, 4),
               (err) => {
                 if (err) throw console.log(err.messages);
               }
             ); */
           });

console.log(res);

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