Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index of object in array to look up corresponding object in other array

I have two arrays. One is an array of names and the other is an array made up of strings titled "Yes" or "No". The index path of each name in the "name" array corresponds with the same index path in the "Yes/No" array. For example:

Names Array | Yes/No Array
Person 1    | Yes
Person 2    | No
Person 3    | Yes

What would be the easiest way to look up a person's name (possibly getting the index path of it) and check whether they are "Yes" or "No" in the "Yes/No" array?

Also, I'm not sure if "index path" is the right term to use. If it isn't, I mean the number that an object is in an array.

like image 347
Preston Avatar asked Jun 18 '11 03:06

Preston


People also ask

How do you find the index of an object in an array of objects?

To find the index of an object in an array, by a specific property: Use the map() method to iterate over the array, returning only the value of the relevant property. Call the indexOf() method on the returned from map array. The indexOf method returns the index of the first occurrence of a value in an array.

How do you find a specific object in an array of objects?

To search a particular object, we will use the Array prototype find method. This returns a value on a given criterion, otherwise, it returns 'undefined'. It takes two parameters, one required callback function and an optional object, which will be set as a value of this inside the callback function.

How do I extract an index from an array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.

Can you use indexOf for an array of objects?

The Array. indexOf() method returns the index of the first matching item in an array (or -1 if it doesn't exist). var wizards = ['Gandalf', 'Radagast', 'Saruman', 'Alatar']; // Returns 1 wizards.


1 Answers

NSArray has a method called indexOfObject that will return either the lowest index whose corresponding array value is equal to anObject or NSNotFound if no such object is found. If your array of names is unsorted, then use this to get the index that you can then plug in to the Yes/No array. That is, something along these lines:

NSString *answer = nil;
NSUInteger index = [namesArray indexOfObject:@"John Smith"];
if (index != NSNotFound) {
    answer = [yesNoArray objectAtIndex:index];
}
return answer;

Because Bavarious asks questions where I assume, here's a better way when the array of names is sorted alphabetically.

int index = [self findName:@"John Smith"];
NSString *answer = nil;
if (index >= 0) {
    answer = [yesNoArray objectAtIndex:index];
}
return answer;

where the function findName is a simple binary search:

-(int)findName:(NSString *)name {
    int min, mid, max;
    NSComparisonResult comparisonResult;
    min = 0;
    max = [namesArray count]-1;
    while (min <= max) {
        mid = min + (max-min)/2;
        comparisonResult = [name compare:[namesArray objectAtIndex:mid]];
        if (comparisonResult == NSOrderedSame) {
            return mid;
        } else if (comparisonResult == NSOrderedDescending) {
            min = mid+1;
        } else {
            max = mid-1;
        }
    }   
    return -1;  
}
like image 61
PengOne Avatar answered Oct 13 '22 01:10

PengOne