0

I'm trying to chain two filters, based in two ranges (arrays) of params that may also be empty, so it would be possible that f.ex. speedlimit=[]

var speedfilter =[240,300] var pricefilter = [80,120] var cars = [ {name:'Ferrari', maxspeed:240, price: 100}, {name:'Porsche', maxspeed:220, price: 90}, {name:'Bugatti', maxspeed:300, price: 500} ]; if (speedfilters) { return cars.filter(function (car) { return car.maxspeed >= speedfilter[0] && car.maxspeed <= speedfilter[1]; }) } else if (pricefilter) { return cars.filter(function (car) { return car.price >= pricefilter[0] && car.price <= pricefilter[1]; }) } else return cars 

The result in the example above should output {name:'Ferrari', speed:240, price: 100}

What would be the way to do it with javascript filter? Thanks in advance!

4
  • is it possible that there is a minimum speed but no maximum speed? Commented Jul 13, 2018 at 15:59
  • Dont use else if, filter cars by speed limit first and assign to a variable, then filter that variable by price limit Commented Jul 13, 2018 at 16:03
  • Maybe the names were confusing, I just changed it. I just want to filter the objects whose maxspeed are within the first range, and then filter the output again with the price Commented Jul 13, 2018 at 16:04
  • 1
    I'm confused by speedlimit='' - that variable is nowhere in your code. Did you mean speedfilter=[]? Commented Jul 13, 2018 at 16:05

2 Answers 2

2

You could create a filterCar method via prototype inheritance

Array.prototype.filterCar = function(feature, range) { return this.filter((el) => { // is the range defined? if (!!range.length) { return el[feature] >= range[0] && el[feature] <= range[1]; } else { return true; } }) }; var cars = [ {name:'Ferrari', speed:240, price: 100}, {name:'Porsche', speed:220, price: 90}, {name:'Bugatti', speed:300, price: 500} ]; var result1 = cars.filterCar('speed', [240, 300]) .filterCar('price', [80, 120])); var result2 = cars.filterCar('speed', [ ]) .filterCar('price', [80, 120])); console.log(result1); // [{name: "Ferrari", speed: 240, price: 100}] console.log(result2); /* [{name: "Ferrari", speed: 240, price: 100}, {name: "Porsche", speed: 220, price: 90}] */ 
Sign up to request clarification or add additional context in comments.

6 Comments

filterCar on the prototype of Array.... doesn't quite feel "right". Could/should have just been a method call. Also doesnt cover the "if exists" requirement.
this is just an example about how to use filter function. Don't understand the downvote btw.
Wasnt mine! I promise.
I didn't downvote, but isn't the main thrust of the question how to handle possibly empty ranges?
@Joe82 it now does via the if...else inside the filter BTW
|
1

You can wrap your filtering up into a re-usable method, and this can account for the filter not being available.

function filterCars(carsArray, property, rangeArray) { // if rangeArray is not supplied, or is empty, just return the unfiltered input if(!rangeArray|| rangeArray.length === 0) { return carsArray; } // otherwise filter according to logic return carsArray.filter(car => car[property] >= rangeArray[0] && car[property] <= rangeArray[1]); } 

This can be chained, or for more readability called in sequence:

 function filterCars(carsArray, property, rangeArray) { if(!rangeArray|| rangeArray.length === 0) { return carsArray; } return carsArray.filter(car => car[property] >= rangeArray[0] && car[property] <= rangeArray[1]); } var speedfilter = []; // [240,300] var pricefilter = [80,120] var cars = [ {name:'Ferrari', maxspeed:240, price: 100}, {name:'Porsche', maxspeed:220, price: 90}, {name:'Bugatti', maxspeed:300, price: 500} ]; cars = filterCars(cars,"maxspeed",speedfilter); cars = filterCars(cars,"price",pricefilter); console.log(cars);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.