I will try my best to explain this. I have an input field which allows the user to enter a certain time. I also have an array of attractions that have times with them. Once the user enters a time and clicks a button, all arrtactions with that time should load to dom. The event listener is working, but every attraction is printing, even the ones without times. Below is what I have so far. I believe I am just missing some minor details.
<input id="timeInput" type="text" placeholder="Ex: 1:00 PM">
<button id="timeBtn">Show me Scheduled Events!</button>
let timeTest = [];
let timesArray = [];
let timeSearch = document.getElementById("timeInput");
$('#timeBtn').on('click',((e) => {
timeTest.push(timeSearch.value);
let timeSplit = timeTest[1].split(":");
let hourSelected = timeSplit[0];
let morningOrEvening = timeSplit[1];
controller.getType()
.then((data) => {
for(let i = 0; i < data.length; i++) {
if (data[i].times !== undefined) {
if (timeValueCheck(data[i].times)) {
timesArray.push(data[i]);
}
else {
timesArray.push(data[i]);
}
}}
timesArray.forEach(attraction =>{
$('#output').append(attrHBS(attraction));
console.log(attraction);
}
);
}
);
}));
let timeValueCheck= (timesArray) => {
let timeSplit = timeTest[1].split(":");
let hourSelected = timeSplit[0];
let morningOrEvening = timeSplit[1];
for (let i=0; i < timesArray.length; i++) {
let splitArray = timesArray[i].split(":");
console.log("super", splitArray);
if (hourSelected === splitArray[0]){
console.log("mega",hourSelected, splitArray[0]);
return true;
}
}
return false;
};
// this is what is inside controller.js
module.exports.getType = (attrData) => {
//creating new Promise to load when used in other functions
return new Promise((resolve, reject) => {
//getting our data from two ajax calls, attractions and attraction types
let p1 = factory.getAttractionData();
let p2 = factory.getAttTypes();
// empty array to push data into once we have manipulated it
let newDataWithTypes = [];
//promise all to get both data types before using them.
Promise.all([p1,p2])
.then((attrData) => {
// loop over the first array, all 132 attractions
attrData[0].forEach(allAttractions => {
// loop over second array, the 8 types
attrData[1].forEach(typeofAttractions => {
// if statement to add the types to each attraction based on their type id!
if (allAttractions.type_id === typeofAttractions.id) {
allAttractions.type = typeofAttractions.name;
// pushes to the array on 32
newDataWithTypes.push(allAttractions);
}
});
});
resolve(newDataWithTypes);
}
);
});
};
Array's index in javascript begins with 0.I think this is the problem. try this
let timeSplit = timeTest[0].split(":");...
instead of
let timeSplit = timeTest[1].split(":");...
该行不应该为“ 0”:
let timeSplit = timeTest[0].split(":")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.