Optimal way to write if and else with minimum of repete?

Optimal way to write if and else with minimum of repete?

Hello!

My questions if about the best way to write the following stuff most optimal with so few lines as posible? I provide an shorten example, in my case it should be much longer (A-Z). I hope you see what I trying to do.

Thanks!


  1. var Alfa =  data.daily.[1].ico; // posible values A-Z
  2. var Beta =  data.daily.[2].ico; // posible values A-Z
  3. var Gamma = data.daily.[3].ico; // posible values A-Z

  4. if (Alfa == 'A') {
  5. $('.testAlfa').attr('data-icon', 'A');
  6. else if (Alfa == 'B') {
  7. $('.testAlfa').attr('data-icon', 'B');
  8. }
  9. else if (Alfa == 'C) {
  10. $('.testAlfa').attr('data-icon', 'C');
  11. }  
  12. else{}

  13. if (Beta == 'A') {
  14. $('.testBeta').attr('data-icon', 'A');
  15. else if (Beta == 'B') {
  16. $('.testBeta').attr('data-icon', 'B');
  17. else if (Beta == 'C') {
  18. $('.testBeta').attr('data-icon', 'C');
  19. }  
  20. else{}

  21. if (Gamma == 'A') {
  22. $('.testGamma').attr('data-icon', 'A');
  23. else if (Gamma == 'B') {
  24. $('.testGamma').attr('data-icon', 'B');
  25. }
  26. else if (Gamma == 'C') {
  27. $('.testGamma').attr('data-icon', 'B');
  28. else{}


  29. <span class="testAlfa" data-icon=""></span>
  30. <span class="testBeta" data-icon=""></span>
  31. <span class="testGamma" data-icon=""></span>