![](/img/trans.png)
[英]In ReactJS todo app I am trying to set input field text state into another state which is array of objects
[英]I am trying to find times in an array of objects using the value of my input field
我會盡力解釋這一點。 我有一個輸入字段,允許用戶輸入特定時間。 我也有許多與時俱進的景點。 一旦用戶輸入時間並單擊一個按鈕,所有與該時間有關的仲裁都應加載到dom中。 事件偵聽器正在運行,但是每個吸引人的對象都在打印,即使沒有時間的對象也是如此。 以下是到目前為止的內容。 我相信我只是缺少一些小細節。
<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);
}
);
});
};
javascript中數組的索引以0開頭。我認為這是問題所在。 嘗試這個
let timeSplit = timeTest[0].split(":");...
代替
let timeSplit = timeTest[1].split(":");...
該行不應該為“ 0”:
let timeSplit = timeTest[0].split(":")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.