1

const products = [{"id":1, "price":1, "bestseller":1,"novelty":1,"discount":5},{"id":2, "price":1, "bestseller":0,"novelty":0,"discount":null},{"id":3, "price":1, "bestseller":0,"novelty":0,"discount":null},{"id":4, "price":1, "bestseller":0,"novelty":0,"discount":null},{"id":5, "price":1,
 "bestseller":0,"novelty":0,"discount":null},{"id":6, "price":1, "bestseller":0,"novelty":0,"discount":null},{"id":7, "price":1, "bestseller":1,"novelty":1,"discount":null},{"id":8, "price":1, "bestseller":1,"novelty":1,"discount":null},{"id":9, "price":1, "bestseller":1,"novelty":1,"discount":null}];

const filters = {
    price: {
        min: 1,
        max: 10,
    },
    promo: {
        bestseller: {
            isChecked: true
        },
        novelty: {
            isChecked: false
        },
        discount: {
            isChecked: true
        }
    }
};

const filteredProducts = products.filter(product => {
if (
    (!filters.price.min || product.price >= filters.price.min) &&
    (!filters.price.max || product.price <= filters.price.max) &&
    (!filters.promo.bestseller.isChecked || filters.promo.bestseller.isChecked && product.bestseller) &&
    (!filters.promo.novelty.isChecked || filters.promo.novelty.isChecked && product.novelty) &&
    (!filters.promo.discount.isChecked || filters.promo.discount.isChecked && product.discount)
    ) return product;
});

console.log(filteredProducts);

Я хочу получить в результате массив из 4 элементов, где значения элементов совпадают с фильтрами из объекта price, и promo с значениями bestseller, discount.

=== UPD: Обновил код.

1
  • isChecked - ??
    – user176262
    12 мая 2021 в 19:51

1 ответ 1

0

Можно получить так.

const products = [{"id":1, "price":1, "bestseller":1,"novelty":1,"discount":5},{"id":2, "price":1, "bestseller":0,"novelty":0,"discount":null},{"id":3, "price":1, "bestseller":0,"novelty":0,"discount":null},{"id":4, "price":1, "bestseller":0,"novelty":0,"discount":null},{"id":5, "price":1,
 "bestseller":0,"novelty":0,"discount":null},{"id":6, "price":1, "bestseller":0,"novelty":0,"discount":null},{"id":7, "price":1, "bestseller":1,"novelty":1,"discount":null},{"id":8, "price":1, "bestseller":1,"novelty":1,"discount":null},{"id":9, "price":1, "bestseller":1,"novelty":1,"discount":null}];

const filters = {
    price: {
        min: 1,
        max: 10,
    },
    promo: {
        bestseller: {
            isChecked: true
        },
        novelty: {
            isChecked: false
        },
        discount: {
            isChecked: true
        }
    }
};

const filteredProducts = products.filter(product => {
        if (
           !filters.promo.bestseller.isChecked &&
           !filters.promo.novelty.isChecked &&
           !filters.promo.discount.isChecked
           ) {
             return product;
           }    


        if ((!filters.price.min || product.price >= filters.price.min) &&
        (!filters.price.max || product.price <= filters.price.max) &&
        ((filters.promo.bestseller.isChecked && product.bestseller) ||
        (filters.promo.novelty.isChecked && product.novelty) ||
        (filters.promo.discount.isChecked && product.discount))) return product;
});

console.log(filteredProducts);

2
  • То же заметил - пропустил "isChecked". Я ожидаю, что каждый элемент из продуктов содержащий "Boolean = true" значения для фильтров, у которых isChecked помечен "true" пройдут условие
    – Лукас
    12 мая 2021 в 20:05
  • @Лукас подправил немного ответ 12 мая 2021 в 20:19

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.