Помогите исправить код для фильтраций JSON данных

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 шт):

Автор решения: Aziz Umarov

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

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);

→ Ссылка