Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter by multiple array conditions

Tags:

swift

The filter method is a really powerful tool for filtering by single or multiple conditions, but is there a way to filter by conditions of arrays?

class Car with properties : model, color, engineStatus.

  • cars is an array with few cars

By one condition would look like:

let currModel = `Opel`

let filterdObject = cars.filter { $0.model == currModel }

By two or more conditions would look like:

let currModel = `Opel`
let currColor = `Green`

let filterdObject = cars.filter { $0.model == currModel || $0.color == currColor }

My question is it how could I filter by an array like:

An array has ,e.g., two colors blue and green. I would like to filter cars by these colors. My point is to get a formula for n-conditions.

like image 298
yerpy Avatar asked Apr 11 '17 14:04

yerpy


People also ask

How do you filter data from an array?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

How do you find multiple values in an array?

To check if multiple values exist in an array:Use the every() method to iterate over the array of values. On each iteration, use the indexOf method to check if the value is contained in the other array. If all values exist in the array, the every method will return true .

Can we write if condition in filter?

The IF statement used within the Filter formula tests whether the criteria cell G1 is blank or not. It works like this. It says every thing. That means 'all the rows in the range' because n(A1:A) returns a range with 0's.


2 Answers

Forget about the filter for a moment. Think how you would check if a car's color is a value in an array.

let colors = [ "Green", "Blue" ]
// or let colors: Set = [ "Green", "Blue" ]
if colors.contains(someCar.color) {
}

Simple enough. Now use that same simple expression in the filter.

let filterdObject = cars.filter { $0.model == currModel || colors.contains($0.color) }
like image 85
rmaddy Avatar answered Oct 12 '22 02:10

rmaddy


Treat the filter closures like a value type and store them in an array. Use the inner reduce call to create a single boolean value that is true is all of the conditions are met by the current car. If you need a compound test like color == "blue" or "green" then simply add that to your filter closure conditions array.

    struct Car {
      let model: String
      let color: String
    }

    let conditions: [(Car) -> Bool] = [
      {$0.model == "Opel"},
      {$0.color == "Red"},
    ]

    let carLot = [
      Car(model: "Opel", color: "Green"),
      Car(model: "Mustang", color: "Gold"),
      Car(model: "Opel", color: "Red"),
    ]

    let allRedOpels = carLot.filter {
      car in
      conditions.reduce(true) { $0 && $1(car) }
   }
like image 32
Price Ringo Avatar answered Oct 12 '22 00:10

Price Ringo