1

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 ?

7
  • 3
    Your function iterates over an array but returns a single value at the end. So, you'd only get the last item translated. Commented Feb 14, 2022 at 18:42
  • yes, do you have the answer to get aal the items translated too? Commented Feb 14, 2022 at 18:44
  • You can return an array instead of a single value. Commented Feb 14, 2022 at 18:45
  • what exactly is the output Commented Feb 14, 2022 at 18:45
  • 1
    another option would be to use a key value pair. easy to extend as well Commented Feb 14, 2022 at 18:54

4 Answers 4

2

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));

Sign up to request clarification or add additional context in comments.

Comments

2

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)

1 Comment

For quality reasons can you use switch instead?
1

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)

Comments

1

You use 1 variable label to assign the value, so it will only assign the last value.

Also a better option is to use switch if you only want to do give value based on expression.

The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.

let indexLabelServices = [' Pear', ' Apple', ' Banana', ' Peach', ' Orange', ' Cherry']
    let result = []
for (let i of indexLabelServices) {
  switch (i) {
      case " Pear":
      result.push("Pera");
      break;
    case " Apple":
       result.push( "Mela");
      break;
    case " Banana":
       result.push("Platano");
      break;
    case " Orange":
       result.push("Arancia");
      break;
     case " Peach":
     result.push("Pesca");
     break
    case " Cherry":
       result.push("Ciliegia");
      break;
  }
}
console.log(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.