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

91
Visualizações
Check string elements in an array to manipulate

I have the following array returning from a service

indexLabelServices = [ ' Pear', ' Apple', ' Banana',' Peach',' Orange',' Cherry' ]

For each element of the array i want to give a different label ( translation )

This is my code

  const convertServicesLabels = (indexLabelServices) => {
    let label="";
    for (let index = 0; index < indexLabelServices.length; ++index) {
      const element = indexLabelServices[index];
      if(element === " Pear){
        label=Pera;
      }else  if(element ===" Apple"){
        label=Mela;
      }else  if(element ===" Banana"){
        label=Platano;
      }else  if(element ===" Peach"){
        label=Pesca
      }else  if(element ===" Orange"){
        label=Arancia;
      }else  if(element ===" Cherry"){
        label=Ciliegia;
      }
    }
    return label;
  }

The result i have with this method is that the only the element Orange is translated to Arancia, not others element get transalated.

What am i doing wrong? How can i manipulate/translate any element of the array indexLabelServices ?

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

0

you are only returning the last element you are storing in label not mutating the array, try using map over the list.

let indexLabelServices = [' Pear', ' Apple', ' Banana', ' Peach', ' Orange', ' Cherry']

indexLabelServices = indexLabelServices.map(element => {
  switch (element.toLocaleLowerCase().trim()) {
    case "pear":return "Pera";
    case "apple":return "Mela";
    case "banana":return "Platano";
    case "peach":return "Pesca";
    case "orange":return "Arancia";
    case "cherry":return "Ciliegia";
    default:return "Unknown";
  }
})

console.log(indexLabelServices)

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem is located within a for loop where you every iteration rewrite variable label. Another problem is iterating over that array. When you do ++index then variable index increment before enter the for loop body so instead try to use index++. In this case will be index increment after one iteration of for loop.

If you need to return array of all translation edit your code to something like this:

  let indexLabelServices = [ ' Pear', ' Apple', ' Banana',' Peach',' Orange',' Cherry' ];
  
  const convertServicesLabels = (indexLabelServices) => {
    let translations = [];
    for (let index = 0; index < indexLabelServices.length; index++) {
      const element = indexLabelServices[index];
      if(element === " Pear"){
          translations[index] = 'Pera';
      }
      else if(element ===" Apple"){
          translations[index] = 'Mela';
      }
      else if(element ===" Banana"){
         translations[index] = 'Platano';
      }
      else if(element ===" Peach"){
          translations[index] = 'Pesca';
      }
      else if(element ===" Orange"){
          translations[index] = 'Arancia';
      }
      else if(element ===" Cherry"){
          translations[index] = 'Ciliegia';
      }
    }
    return translations;
  }
  
  console.log(convertServicesLabels(indexLabelServices));

about 4 years ago · Juan Pablo Isaza Relatório

0

Set up an object containing the translations, then map the original array to a new array using the array elements to return the translation from the dictionary.

const indexLabelServices = [' Pear', ' Apple', ' Banana', ' Peach', ' Orange', ' Cherry'];
const dict = {' Pear':'Pera', ' Apple':'Mela', ' Banana':'Platano', ' Peach':'Pesca', ' Orange':'Arancia', ' Cherry':'Ciliegia'};

console.log(dict);
let translations = indexLabelServices.map(x=>dict[x]);
console.log(translations)

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