简体   繁体   中英

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

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>

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.

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