Checking If an Array of Custom Objects Contain a Specific Custom Object

Checking if an array of custom objects contain a specific custom object

There are two contains functions:

extension SequenceType where Generator.Element : Equatable {
/// Return `true` iff `element` is in `self`.
@warn_unused_result
public func contains(element: Self.Generator.Element) -> Bool
}

extension SequenceType {
/// Return `true` iff an element in `self` satisfies `predicate`.
@warn_unused_result
public func contains(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Bool
}

The compiler is complaining because the compiler knows that Person is not Equatable and thus contains needs to have a predicate but alex is not a predicate.

If the people in your array are Equatable (they aren't) then you could use:

person.list.contains(alex)

Since they aren't equatable, you could use the second contains function with:

person.list.contains { $0.name == alex.name }

or, as Martin R points out, based on 'identity' with:

person.list.contains { $0 === alex }

or you could make Person be Equatable (based on either name or identity).

Swift: Determine if a array of custom objects contains a particular string

Add something like this

func contains(make: String) -> Bool {
carArray.compactMap(\.make).contains(make)
} // returns true in your case for "BMW"

This has two parts. First, I am mapping the array of Car objects to an array of String? by mapping over the make key path. Since this may have nil values, I am using compactMap to remove the nil values.

If you are going to be doing this a lot and don't want the overhead of mapping and checking every time then create something to store your cars. Something like this:

struct Car {
var make: String?
var model: String?
var year: Double?
}

struct CarStore {
private(set) var storage: [Car] = .init()
private var makes: Set<String> = .init()

mutating func add(car: Car) {
storage.append(car)
car.make.map { makes.insert($0) } // map to run the closure only if make is not nil
}

func contains(make: String) -> Bool {
makes.contains(make)
}
}

var store = CarStore()

store.add(car: Car(make: "Audi", model: "S5", year: 2015))
store.add(car: Car(make: "BMW", model: "X3", year: 2016))
store.add(car: Car(make: "Honda", model: "Accord", year: 2018))

store.contains(make: "BMW") // -> true

How to check if already custom object exist in array?

if !self.arrPerson.contains(where: {($0.id == value.id)}){
self.arrPerson.append(value)
}

Check if an array of custom objects contains an object with a certain date

I'm not very well versed with KVC, but if the solution doesn't need to use KVC, you can just iterate:

NSDate *dateToCompare = ...;
BOOL containsObject = NO;
for (MyEvent *e in matches)
{
if ([e.eve_date isEqualToDate:dateToCompare])
{
containsObject = YES;
break;
}
}

if (containsObject) NSLog(@"Contains Object");
else NSLog(@"Doesn't contain object");

I've had a play about with KVC and attempted a solution with that too. You were only missing valueForKeyPath instead of valueForKey

if ([[matches valueForKeyPath:@"eve_date"] containsObject:d])
{
NSLog(@"Contains object");
}
else
{
NSLog(@"Does not contain object");
}

Check if Swift array contains instance of object

Use the identity operator

static func ==(lhs: Car, rhs: Car) -> Bool {
return lhs === rhs
}

Or even without Equatable

myCars.contains{ $0 === mazda }

Edit: But a better solution is to implement equality rather than identity, for example a vendor and type property in the class

static func ==(lhs: Car, rhs: Car) -> Bool {
return lhs.vendor == rhs.vendor && lhs.type == rhs.type
}

How to determine if Javascript array contains an object with an attribute that equals a given value?

2018 edit: This answer is from 2011, before browsers had widely supported array filtering methods and arrow functions. Have a look at CAFxX's answer.

There is no "magic" way to check for something in an array without a loop. Even if you use some function, the function itself will use a loop. What you can do is break out of the loop as soon as you find what you're looking for to minimize computational time.

var found = false;
for(var i = 0; i < vendors.length; i++) {
if (vendors[i].Name == 'Magenic') {
found = true;
break;
}
}

Swift - Check if array contains element with property

Yes,

if things.contains(where: { $0.someProperty == "nameToMatch" }) {
// found
} else {
// not
}


Related Topics



Leave a reply



Submit