简体   繁体   English

我正在尝试使用输入字段的值在对象数组中查找时间

[英]I am trying to find times in an array of objects using the value of my input field

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. 一旦用户输入时间并单击一个按钮,所有与该时间有关的仲裁都应加载到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. javascript中数组的索引以0开头。我认为这是问题所在。 try this 尝试这个

let timeSplit = timeTest[0].split(":");...

instead of 代替

let timeSplit = timeTest[1].split(":");...

该行不应该为“ 0”:

let timeSplit = timeTest[0].split(":")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ReactJS 待办事项应用程序中,我正在尝试将输入字段文本 state 设置为另一个 state 对象数组 - In ReactJS todo app I am trying to set input field text state into another state which is array of objects 我正在使用 select 搜索表单,我试图在输入文本字段中显示选定的值 - I am using select search form and i trying to display selected value in to the input text field 我正在尝试使用我创建的react函数基于键和值参数对对象数组进行排序 - I am trying to sort an array of objects based on a key and value parameter using react functions that I created 我正在尝试检查给定值是否作为对象数组中的键存在 - I am trying to check if given value exist as key in array of objects 为什么我无法使用jQuery获取此输入字段的值? - Why am I unable to get the value of this input field using jQuery? 我正在尝试在JavaScript中将骰子滚动1000次,但我的数组破坏了NaN - I am trying to roll the dice 1000 times in JavaScript but my array returms a NaN 我无法使用 JavaScript 访问我的 canvas 上的输入值 - I am not able to access input value on my canvas using JavaScript 尝试从输入字段返回日期值时,我得到了未定义 - I am getting undefined when trying to return the date value from an input field 我正在使用react js和jsx并试图获得一个简单的搜索输入字段以在单击时增长 - I am using react js and jsx and trying to get a simple search input field to grow when clicked 为什么我在加号和减号按钮的输入字段中获得0作为我的值 - Why I am getting 0 as my value from an input field in a plus and minus button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM