0

My code is as follows :

    import { filter_names } from './search/filterActions';

export const string_names = {
    diesel: 'diesel',
    benzine: 'benzine',
    transmissionManual: 'manual',
    transmissionAutomatic: 'automatic'
};

function filterItem(filter, car) {
    switch(filter.name) {

        case filter_names.priceRange:
            return filter.values.filter((v) => v.active).every((v) => (car.price <= v.high && car.price >= v.low));
        case filter_names.mileage:
            return filter.values.filter( v => v.active).every( v => (car.mileage >= v.low && car.mileage <= v.high));
        case filter_names.year:
            return filter.values.filter(v => v.active).every(v => (parseInt(car.initialRegistration.substr(car.initialRegistration.length - 4)) >= v.low && parseInt(car.initialRegistration.substr(car.initialRegistration.length - 4)) <= v.high));
        case filter_names.fuel:
            return filter.values.every(v => {
                if(v.checkboxBenzine && v.checkboxDiesel) return car;
                if(v.checkboxBenzine) return car.fuel.toLowerCase() == string_names.benzine;
                if(v.checkboxDiesel) return car.fuel.toLowerCase() == string_names.diesel;
            });
        case filter_names.transmission:
            return filter.values.every(v => {
                if(v.checkboxManual && v.checkboxAutomatic) return car;
                if(v.checkboxAutomatic) return car.transmission.toLowerCase() == string_names.transmissionAutomatic;
                if(v.checkboxManual) return car.transmission.toLowerCase() == string_names.transmissionManual;
            });
        case filter_names.make:
            //console.log(filter);
            //console.log(car);
            console.log(filter.values.filter(v => v).every(v => car.make.indexOf(v.model) !== -1));
            return true;
        default:
            return true;
    }
}

export function filterCars(cars, filters){
    if(!filters) return cars;
    return cars.filter((car) => filters.every((filter) => filterItem(filter, car)));
}

The parameters filter and cars are as follows :

let filter = {
  name: "MAKE", values: [{ Golf: {active: true, make: "Volkswagen", model: "Golf"},
                           Passat: {active: true, make: "Volkswagen", model: "Passat"},
                           Q5: {active: true, make: "Audi", model: "Q5"}
                         }
                        ]
}

let cars = [
  {id: 20,make: "Audi"},
  {id: 21, make: "Skoda"},
  {id: 22,make: "Audi Q5"},
  {id: 23,make: "A1"},
  {id: 24,make: "Volkswagen Skoda"},
  {id: 25,make: "Audi A7"},
  {id: 26,make: "Audi A5"}  
]

How can I loop through filters and check if any model from filter values exists in cars make?

I have problem in filter_names.make case.

The wanted result is in this case : [{id: 22,make: "Audi Q5"}]

1
  • Loop through filter.values and check each model with cars.make. e.g. cars.make == this.model Commented Sep 8, 2016 at 7:33

1 Answer 1

1

According to your condition:

"check if any model from filter values exists in cars make"

The solution using Object.keys and Array.some functions:

var values = filter.values[0],
    carExists = Object.keys(values).some(function (k) {
        return (values[k].make + " " + values[k].model) === cars.make;
    });

console.log(carExists);  // true
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate. I changed it a little bit let values = filter.values[0]; return Object.keys(values).some(v => car.make.indexOf(values[v].model) !== -1); and it works like it should.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.