is there a possibility to get an object from an array with an specific property? Or do i need to loop trough all objects in my array and check if an property is the specific i was looking for?
edit: Thanks for given me into the correct direction, but i have a problem to convert this.
// edit again: A ok, and if there is only one specific result? Is this also a possible method do to that?
let imageUUID = sender.imageUUID let questionImageObjects = self.formImages[currentSelectedQuestion.qIndex] as [Images]! // this is working //var imageObject:Images! /* for (index, image) in enumerate(questionImageObjects) { if(image.imageUUID == imageUUID) { imageObject = image } } */ // this is not working - NSArray is not a subtype of Images- so what if there is only 1 possible result? var imageObject = questionImageObjects.filter( { return $0.imageUUID == imageUUID } )
Answer: Use the find() Method You can simply use the find() method to find an object by a property value in an array of objects in JavaScript. The find() method returns the first element in the given array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
The hasOwnProperty() method will check if an object contains a direct property and will return true or false if it exists or not. The hasOwnProperty() method will only return true for direct properties and not inherited properties from the prototype chain.
Dot Notation for Accessing Object Properties Properties of a JavaScript object can be accessed using the dot notation in this manner: object. propertyName . Nested properties of an object can be accessed by chaining key names in the correct order.
// this is not working - NSArray is not a subtype of Images- so what if there is only 1 possible result?
You have no way to prove at compile-time that there is only one possible result on an array. What you're actually asking for is the first matching result. The easiest (though not the fastest) is to just take the first element of the result of filter:
let imageObject = questionImageObjects.filter{ $0.imageUUID == imageUUID }.first
imageObject
will now be an optional of course, since it's possible that nothing matches.
If searching the whole array is time consuming, of course you can easily create a firstMatching
function that will return the (optional) first element matching the closure, but for short arrays this is fine and simple.
As charles notes, in Swift 3 this is built in:
questionImageObjects.first(where: { $0.imageUUID == imageUUID })
Edit 2016-05-05: Swift 3 will include first(where:)
.
In Swift 2, you can use indexOf
to find the index of the first array element that matches a predicate.
let index = questionImageObjects.indexOf({$0.imageUUID == imageUUID})
This is bit faster compared to filter
since it will stop after the first match. (Alternatively, you could use a lazy
sequence.)
However, it's a bit annoying that you can only get the index and not the object itself. I use the following extension for convenience:
extension CollectionType { func find(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Self.Generator.Element? { return try indexOf(predicate).map({self[$0]}) } }
Then the following works:
questionImageObjects.find({$0.imageUUID == imageUUID})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With