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

148
Visualizações
JS: Extracting data from an array of objects

I have a complex query with 100s of fields and nested fields. What I want to do is, for each Index, extract the English and French text. As you can see in the array, there is no French text for some indexes. In that case I want to get the English text. For me extracting the English text works fine because the text is already there, but incase of French, I get undefined errors. What would be the best way to implement this. Is Loadash needed for this or just pure JS methods?

Just to be clear, I have erros with extracting french because in some fields, french text is not available, I want to use the english value in that case.

Also It is recommend if I am able to get the English and French values by it's language field rather than the index. I have no idea how to do that.

Any suggestion, documentation is appreciated. Thank you!

example array:

[
 {
    id: "1",
    name: [
      {
        language: "en-US",
        text: "HOLIDAY"
      }
    ],
    order: 6,
    Groups: [
      {
        name: [
          {
            language: "en-US",
            text: "REGULAR"
          }
        ],
        code: "REGEARN"
      },
      {
        name: [
          {
            language: "en-US",
            text: "CHARGE"
          }
        ],
        code: "CHARGE"
      }
    ]
  }
]

and here is the code sandbox that reproduces my error: CODE SAND BOX https://codesandbox.io/s/javascript-forked-5073j

EDIT:

EXPECTED OUTPUT:

{
  key: key,
  englishtext: "Value Here",
  frenchtext: "Value Here"
}

below is a working code, but issue is it does not work when there is no french language or that field. I get undefined errors. So is it possible I can get the needed data from the language field?

x.map((y) => ({
    key: y.id,
    name: y.name[0].text,
    groupname: y.Groups ? x.Groups[0].name?.[0].text : 'N/A',
  }))
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Do you expect result like this? If you don't mind lodash.

const _ = require('lodash');

const getNames = (arr) => {
  return arr.map((obj) => {
    const id = obj.id;
    const englishtext = _.get(obj, 'name[0].text', 'N/A');
    const frenchtext = _.get(obj, 'name[1].text', englishtext);

    return { id, englishtext, frenchtext };
  });
};
        
console.log(getNames(x));
// [
//   { id: '1', englishtext: 'HOLIDAY', frenchtext: 'HOLIDAY' },
//   { id: '2', englishtext: 'Stat Holiday', frenchtext: 'Congé Férié' },
//   { id: '3', englishtext: 'Over', frenchtext: 'Over' }
// ] 
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