0

I need to show specific array data from my global array.

var objSport = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Hiking', 'Fishing']; //Global Array Sport
var objTime = ['10:00', '12:00', '14:00', '16:00', '19:00', '18:00']; //Global Array Sport Time

var wantToShow = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Fishing']; //Without Hiking 19:00

So on that code above, I need to take out/hide Hiking with time 19:00.

Here is the loop JS:

for(var i in objSport)
{
    var newOption = $('<th class="th"><div class="name">'+wantToShow[i]+'</div><div class="time">'+objTime[i]+'</div></th>');
    $('.tblSport').append(newOption);
}

and table HTML:

<table class="tblSport"></table>

I tried to run the code, Hiking is hide now but the time is not hide.

var wantToShow = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Fishing']; //without hiking
var objSport = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Hiking', 'Fishing'];
var objTime = ['10:00', '12:00', '14:00', '16:00', '19:00', '18:00'];

for(var i in objSport)
{
    var newOption = $('<th class="th"><div class="name">'+wantToShow[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>
HiDayurie Dave
  • 1,791
  • 2
  • 17
  • 45
  • You should be careful when using `for .. in` over an array; see [Why is using “for…in” for array iteration a bad idea?](https://stackoverflow.com/q/500504/215552) – Heretic Monkey Dec 29 '20 at 03:26

3 Answers3

1

Here is the code, one more if statement check.

var wantToShow = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Fishing']; //without hiking
var objSport = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Hiking', 'Fishing'];
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>
Kevin Zhang
  • 1,012
  • 6
  • 14
1

you should be able to do this

var wantToShow = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Fishing'];
var objSport = ['Football', 'Rugby', 'Tennis', 'Badminton', 'Hiking', 'Fishing'];
var objTime = ['10:00', '12:00', '14:00', '16:00', '19:00', '18:00'];

objSport.forEach((sport, i) => {
    if (wantToShow.includes(sport)) {
        var newOption = $('<th class="th"><div class="name">'+ sport +'</div><div class="time">'+objTime[i]+'</div></th>');
        $('.tblSport').append(newOption);
    }
});
D. Seah
  • 4,472
  • 1
  • 12
  • 20
0

You can use a array object. Can as code:

var objSport = [{
    name: 'Football',
    time: '10:00'
  },
  {
    name: 'Rugby',
    time: '12:00'
  },
  {
    name: 'Tennis',
    time: '14:00'
  },
  {
    name: 'Badminton',
    time: '16:00'
  },
  {
    name: 'Hiking',
    time: '19:00'
  },
  {
    name: 'Fishing',
    time: '18:00'
  }
];

objSport.forEach((item) => {
    var newOption = $('<th class="th"><div class="name">'+ item.name +'</div><div class="time">'+ item.time + '</div></th>');
    $('.tblSport').append(newOption);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="tblSport"></table>