1

I'm continuing with previous question

On this below code,

var wantToShow = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Fishing']; //without hiking
var objSport = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Hiking', 'Fishing'];
var objSportIDLanguange = ['Sepakbola', 'Ragbi', 'Tenis', 'Bulu Tangkis', 'Mendaki', 'Memancing'];
var objTime = ['10:00', '12:00', '14:00', '16:00', '19:00', '18:00'];

for(var i in objSport)
{
    if(wantToShow.indexOf(objSport[i]) > -1) {
      var newOption = $('<th class="th"><div class="name">'+objSport[i]+'</div><div class="time">'+objTime[i]+'</div></th>');
    $('.tblSport').append(newOption);
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<table class="tblSport"></table>

Now I have another variable called var objSportIDLanguange = ['Sepakbola', 'Ragbi', 'Tenis', 'Bulu Tangkis', 'Mendaki', 'Memancing']

It's to translate from English to Indonesia.

So on below code I put some code:

var statusTranslate  = "Y";
for(var i in objSport)
{
    if(wantToShow.indexOf(objSport[i]) > -1) {
      if(statusTranslate = "Y")
      {
          //Here I need the var objSportIDLanguange value
      }
      else
      {
          var newOption = $('<th class="th"><div class="name">'+objSport[i]+'</div><div class="time">'+objTime[i]+'</div></th>');
      }
      $('.tblSport').append(newOption);
    }
}
HiDayurie Dave
  • 1,791
  • 2
  • 17
  • 45

1 Answers1

1

You can use statusTranslate == "Y" ? objSportIDLanguange[i] : objSport[i] this will return objSportIDLanguange value if Y else other value then pass this to your div .

Demo Code :

var wantToShow = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Fishing']; //without hiking
var objSport = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Hiking', 'Fishing'];
var objSportIDLanguange = ['Sepakbola', 'Ragbi', 'Tenis', 'Bulu Tangkis', 'Mendaki', 'Memancing'];
var objTime = ['10:00', '12:00', '14:00', '16:00', '19:00', '18:00'];

var statusTranslate = "Y";
for (var i in objSport) {
  if (wantToShow.indexOf(objSport[i]) > -1) {
  //check if sttus transalte is y then use sprtlange else other
    var value = statusTranslate == "Y" ? objSportIDLanguange[i] : objSport[i]
//pass same
    var newOption = $('<th class="th"><div class="name">' + value + '</div><div class="time">' + objTime[i] + '</div></th>');

    $('.tblSport').append(newOption);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<table class="tblSport"></table>
Swati
  • 28,069
  • 4
  • 21
  • 41
  • Hi, 1 question how about if I set like this `var objSportIDLanguange = ['Sepakbola', 'Ragbi', 'Tenis', 'Bulu Tangkis', 'Memancing'];` without `Mendaki`. I mean only value `var wantToShow` will be translate. Not in global array `objSport` – HiDayurie Dave Dec 29 '20 at 08:07
  • Please have a look on this fiddle: https://jsfiddle.net/c2kgofp1/ – HiDayurie Dave Dec 29 '20 at 08:30
  • Hi, wantToShow will be in english only ? because `objSportIDLanguange` is in another language they will not match – Swati Dec 29 '20 at 08:49
  • Hi wantToShow can be English or Indonesia. The point is only translate with requirement `if (wantToShow.indexOf(objSport[i]) > -1)` – HiDayurie Dave Dec 29 '20 at 08:53
  • so if `statusTranslate` is `y` then `wantToShow` will be another language and if `n` then `wantToShow` will be in english ? am i right ? – Swati Dec 29 '20 at 08:56
  • Yes, like that what I mean – HiDayurie Dave Dec 29 '20 at 08:57
  • I am still not sure if i understood or not what you are trying to say. But , check [this](https://jsfiddle.net/m9qs4ytc/) fiddle . – Swati Dec 29 '20 at 09:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226549/discussion-between-hidayurie-dave-and-swati). – HiDayurie Dave Dec 29 '20 at 09:04