I have an array that has several people, each with a phone number.
I want to save the names in the result variable when I type part of the name, either in uppercase or lowercase, and click the search button, and circle the results in the showResultSearch function and display them on the console.
Please help me to get the result
I want to get the result as I wrote it and use the this keyword in writing the function I want to get the result this way
this.array = [
{ name: "Niloufar", phone: 123456 }, { name: "sara", phone: 123456 },
{ name: "Amir", phone: 123456 }, { name: "arman", phone: 123456 },
{ name: "Paria", phone: 123456 }, { name: "nadiya", phone: 123456 }
];
const self = this
this.search = function search() {
function filterArray() {
const selectInput = document.getElementById("inputSearch").value;
const result = self.array.find(item => item.name === selectInput);
if (result === undefined) {
alert("No search results found");
}
}
return filterArray;
}
this.showResultSearch = function () {
const searchResult = self.search;
searchResult.forEach(ele => {
console.log(`name: ${ele.name} phone: ${ele.name}`)
})
}
this.startSearch = function () {
this.search();
this.showResultSearch();
}
<form>
<input type="text" placeholder="Search" id="inputSearch">
<button type="button" id="BtnSearch" onclick="this.startSearch">Search</button>
</form>