Answer the question
In order to leave comments, you need to log in
How to filter cards correctly?
How to filter cards correctly
The filter is made in the form
Data
[
{"id": 1,
"title": "Оболонь",
"descr": "сдан",
"metro": "Минская",
"address": "пр. Оболонский, 26",
"location": {"id": 3, "loc": "< 10"},
"status": {"id": 2, "status": "Сдан"},
"services": true,
"options": [
{"id": 1, "option": "Двор без машин"},
{"id": 3, "option": "Панорамные окна"},
{"id": 4, "option": "Есть кладовые"}
]}, и так 10 карточек ...
//Отправка формы
form.addEventListener('submit', event => {
event.preventDefault();
//Формируем объект с данными
const obj = {
toggle: false
};
//Term
const term = form.elements['term'];
obj.term = term.value;
//Options checkboxes
const options = form.querySelectorAll('.checkbox__real');
const op = [];
options.forEach(opt => {
if(opt.checked){
op.push({[opt.name]: opt.value});
}
});
obj.options = op;
//Toggle checkbox
const toggle = form.elements.comission;
if(toggle.checked){
toggle.value = true;
obj.toggle = Boolean(toggle.value);
}
console.log(obj);
//Фильтрация работает
const f1 = arr.filter(item => {
return item.services === obj.toggle;
});
//Фильтрация дополнительные опции ПРОБЛЕМА
const f2 = arr.filter(item => {
//Хочу получить сравнения наподобии
return item.options[].id === obj.options[].id;
});
console.log(f1);
//Удаляем все карточки
document.querySelectorAll('.card-link').forEach(card => {
card.remove();
});
//Создаем карточки на основе фильтрации
f1.forEach(item => {
createCard(item);
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question