Мне нужно иметь возможность искать в массиве объектов. У меня есть HTML-код для:
<form action = "#" id = "filters">
<label for = "search">Search</label>
<input type = "search" name = "search" id = "search"/>
</form>
<div id = "searchresult"></div>
Понятия не имею, с чего начать, может кто-нибудь мне помочь? Заранее спасибо!



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Попробуйте использовать array.filter (функция (currentValue, index, arr), thisValue)
Не раскрывая полный код и не говоря, вот и вы, я попытаюсь сориентировать вас, чтобы вместо этого вы могли придумать собственное решение. выполните примерно следующие шаги:
Надеюсь, это даст вам представление о том, что делать. Это может быть источник вдохновения для вас
Есть несколько способов добиться того, что вы пытаетесь сделать.
Одним из способов было бы присоединить событие input к полю ввода, чтобы при изменении значения поля ввода вы могли получить значение поля ввода, а затем использовать метод фильтр для фильтрации массива meals на основе значения поля ввода. . Наконец, вы можете отобразить отфильтрованные результаты в searchresult div.
const meals = [
{
id: 1,
title: 'Strawberry Salad with Poppy Seed Dressing',
img: 'Strawberry-Salad-with-Poppy-Seed-Dressing.jpg',
book: 1
},
{
id: 2,
title: 'Cashew Turkey Salad Sandwiches',
img: 'turkey-sandwich.jpg',
book: 2
}
];
const searchField = document.querySelector('#search');
const searchResultsContainer = document.querySelector('#searchresult');
searchField.addEventListener('input', (e) => {
// if input field is empty, clear the search results
if (e.target.value === '') {
searchResultsContainer.innerHTML = '';
return;
}
// filter the meals array
const searchResults = meals.filter(meal => {
return meal.title.toLowerCase().includes(e.target.value.toLowerCase());
});
// before displaying the search results, clear the search results div
searchResultsContainer.innerHTML = '';
// display the titles of the meal objects that include the text entered in input field
searchResults.forEach((element, index) => {
const p = document.createElement('p');
p.textContent = (index + 1) + '. ' + element.title;
searchResultsContainer.appendChild(p);
});
});<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<meta name = "viewport" content = "width=device-width">
<title>JS Bin</title>
</head>
<body>
<form action = "#" id = "filters">
<label for = "search">Search</label>
<input type = "search" name = "search" id = "search"/>
</form>
<div id = "searchresult"></div>
</body>
</html>