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

172
Visualizações
Pushing objects to an array in node js

'both' is my object that should hold 2 arrays: eng, fr.

These arrays each, hold multiple objects.

How do I push every value from frDisplayData to fr:[] in my 'both' object:

const displayData = [];
var both = {eng:displayData, fr:[]};
displayData.forEach(function (item) {
  // You must call .then on the promise to capture the results regardless of the promise state (resolved or still pending)
  translateText(item.explanation, 'fr').then(function(result) {
    frExplanation = result;
    frTitle = translateText(item.title, 'fr').then(function(result) {
      frTitle = result;
      frDisplayData = {
          date: item.date,
          explanation: frExplanation,
          hdurl: item.hdurl,
          media_type: item.media_type,
          service_version: item.service_version,
          tite: frTitle,
          url: item.url
        }
      console.log(frDisplayData);
    })
  });
})

Any simplified solution to this would also help.

Currently, console.log(frDisplayData); gives me the following:

 {
  date: '2022-03-26',
  explanation: "xyz",        
  hdurl: 'xyz.jpg',
  media_type: 'image',
  service_version: 'v1',
  tite: 'Pluton la nuit',
  url: 'xyz.jpg'
}
{
  date: '2022-03-27',
  explanation: "abc",        
  hdurl: 'abc.jpg',
  media_type: 'image',
  service_version: 'v1',
  tite: 'Titan Seas reflète la lumière du soleil',
  url: 'abc.jpg'
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

From what I understand, you have an array displayData containing data in English and you want translate title and explanation properties in each element to French and store in fr variable.

You can use forEach loop on display data and async, await on promise to get much cleaner code.

const displayData = [
  {
    title: "title1",
    explanation: "exp1",
    // other props
  },
];

const fr = [];

// use async callback func in foreach
displayData.forEach(async (each) => {
  // create a copy of eng element
  const eachfr = { ...each };
  
  // use await operators to for translation
  // translate required properties to french
  eachfr.title = await translate(each.title, 'fr');
  eachfr.explanation = await translate(each.explanation, 'fr');
  
  fr.push(eachfr);
});

const both = { eng: displayData, fr: fr };
about 4 years ago · Juan Pablo Isaza Relatório

0

"fr" is a key within the "both" object. All you would have to do is add both.fr.push(frDisplayData) within your "forEach" loop.

const displayData = [];
var both = {eng:displayData, fr:[]};
displayData.forEach(function (item) {
  // You must call .then on the promise to capture the results regardless of the promise state (resolved or still pending)
  translateText(item.explanation, 'fr').then(function(result) {
    frExplanation = result;
    frTitle = translateText(item.title, 'fr').then(function(result) {
      frTitle = result;
      frDisplayData = {
          date: item.date,
          explanation: frExplanation,
          hdurl: item.hdurl,
          media_type: item.media_type,
          service_version: item.service_version,
          tite: frTitle,
          url: item.url
        }
      console.log(frDisplayData);
      both.fr.push(frDisplayData);
    })
  });
})

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