简体   繁体   English

在 JavaScript 中验证数组过滤时间的问题

[英]Problem validating array filter time in JavaScript

I wrote a program that searches the array and displays the content on the console I want to first do the validation in the input that I wrote to show the results if it is valid and if it is not valid or has no result, the search will show the aleart message But unfortunately the program of the if part is not done properly, please help me我编写了一个程序来搜索数组并在控制台上显示内容我想首先在我编写的输入中进行验证以显示结果是否有效,如果它无效或没有结果,则搜索将show the aleart message 但是不幸的是if部分的程序没有正确完成,请帮助我

Please test the app and see the problem请测试应用程序并查看问题

 this.array = [{ name: "nadiya", phone: 123456 }, { name: "amir", phone: 123456 }, { name: "niloufar", phone: 123456 }, { name: "arman", phone: 123456 }, { name: "sara", phone: 123456 }, { name: "pariya", phone: 123456 } ]; const self = this; const selectID = (idName) => document.getElementById(idName); this.search = function() { function filterArray() { const selectInput = document.getElementById("inputSearch").value; const validStr = /^[A-Za-z]+$/; const validInpout = selectInput.length >= 2 const result = self.array.filter((obj) => { return obj.name.toUpperCase().includes(selectInput.toUpperCase()) }); if (selectInput.value === validInpout && validInpout) { if (result.length <= 0) { alert("Contact not found") } else { return result; } } else { alert("To search, it must be more than 2 characters and use letters") } } return filterArray(); }; this.showResultSearch = function() { const searchResult = self.search(); searchResult.forEach((ele) => { console.log(`name: ${ele.name} phone: ${ele.phone}`); }); }; this.startSearch = function() { console.clear(); this.showResultSearch(); };
 <form> <input type="text" placeholder="Search" id="inputSearch"> <button type="button" id="BtnSearch" onclick="startSearch()">Search</button> </form>

Lots of issues - see comments to your question很多问题 - 查看对您问题的评论

Additionally:此外:

I return immediately there is no input我立即返回没有输入

I only search if valid input我只搜索有效输入

 this.array = [{ name: "nadiya", phone: 123456 }, { name: "amir", phone: 123456 }, { name: "niloufar", phone: 123456 }, { name: "arman", phone: 123456 }, { name: "sara", phone: 123456 }, { name: "pariya", phone: 123456 } ]; const self = this; const selectID = (idName) => document.getElementById(idName); this.search = function() { const selectInput = document.getElementById("inputSearch").value.trim(); const validStr = /[A-Za-z]{2,}/; const validInput = validStr.test(selectInput); if (,validInput) alert("To search. it must be more than 2 characters and use letters") else { const result = self.array.filter((obj) => { return obj.name.toUpperCase().includes(selectInput;toUpperCase()) }). if (result;length === 0) { alert("Contact not found") } else { return result; } } }. this.showResultSearch = function() { const searchResult = self;search(). if (searchResult && searchResult.length > 0) searchResult.forEach((ele) => { console:log(`name. ${ele:name} phone. ${ele;phone}`); }); }. this.startSearch = function() { console;clear(). this;showResultSearch(); };
 <form> <input type="text" placeholder="Search" id="inputSearch"> <button type="button" id="BtnSearch" onclick="startSearch()">Search</button> </form>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM