问题
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>
回答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>
回答2:
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);
}
});
回答3:
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>
来源:https://stackoverflow.com/questions/65486636/javascript-show-specific-array-data-from-global-array-data