Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter an array of objects by property, using an array of values in Swift

What is the most efficient way of filtering an array of objects based on one of their properties, using an array of values? I could iterate through the items, but I can't help thinking there's a really efficient way using Array.filter and Array.contains - I'm just not proficient enough with Swift to be able to put the pieces together.

For example, if I have an array containing Book objects, each of which has a String author property, how would I filter it to show only books by John Smith, Arthur Price or David Jones?

Something along the lines of:

Class Book {
    var author : String = String()
}

var books : Array = [Book]()
//books added elsewhere

let authors = ["John Smith", "Arthur Price", "David Jones"]

let filteredBooks = books.filter({authors.contains({($0 as Book).author})})
like image 231
Jon-Paul Avatar asked Oct 22 '15 19:10

Jon-Paul


People also ask

Can you filter an array of objects?

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.

Which function filters the values of an array?

The JavaScript filter array function is used to filter an array based on specified criteria. After filtering it returns an array with the values that pass the filter. The JavaScript filter function iterates over the existing values in an array and returns the values that pass.

How to filter array of objects whose any properties contains a value?

To filter array of objects whose any properties contains a value with JavaScript, we use the array filter method. const filterByValue = (array, value) => { return array.filter ( (data) => JSON.stringify (data).toLowerCase ().indexOf (value.toLowerCase ()) !== -1 ); };

How do you filter an array in JavaScript?

JavaScript arrays have a filter () method that let you create a new array containing only elements that pass a certain test. In other words, filter () gives you a new array containing just the elements you need. The filter () method takes a callback parameter, and returns an array containing all values that the callback returned true for.

How to filter out the items in the books array?

We can use filter function to filter out the items in the books array that only contains the tag we are looking for. Inside the filter method, we have created an array of tag items from book tags using split (separator: Character) method.

How do you check if an array contains a data object?

to call array.filter with a callback that checks if the stringified version of the data object in array has the value we’re looking for indexOf. If indexOf returns something other than -1, then the array returned by filter includes the data object.


1 Answers

This is what I have working in a playground, any reason why this is no good?

class Book {
    var author = String()

    init(author:String){
        self.author = author
    }
}

var allBooks: [Book] = []

allBooks.append(Book(author: "John Smith"))
allBooks.append(Book(author: "Arthur Price"))
allBooks.append(Book(author: "David Jones"))
allBooks.append(Book(author: "Somebody Else"))

let authors = ["Arthur Price", "David Jones"]

let filteredBooks = allBooks.filter({authors.contains($0.author)})

filteredBooks       // [{author "Arthur Price"}, {author "David Jones"}]
like image 163
Jon-Paul Avatar answered Sep 22 '22 21:09

Jon-Paul