I'm referring to the key.
For example, I want to display a list of movie genres that's derived from a
list of movies. Many movies have the same genre. But I wanted a view that
contained the keys that were the unique Genre names of all the movies in
the database.
Before I added the dictionary lookup, I ended up with a list of keys that
were duplicates. Because as the map block reads the documents, it emits the
value of the Genre field into the view as the key and if it encounters the
same value more than once, I was getting duplicates. So now I just build up
a dictionary that makes lookups quick so I know to emit a value or not.
But I thought maybe there was a better way.
Here's my code now:
NSMutableDictionary *existingValues = [[NSMutableDictionary alloc]
init];
[fieldValuesView setMapBlock: MAPBLOCK({
NSDictionary *valuesData = doc[@"values"];
if (valuesData) {
NSString *value = valuesData[weakSelf.field.document.
documentID];
if (!existingValues[value]) {
if ([NSString ts_isNotEmptyString:value]) {
emit(value, nil);
existingValues[value] = @YES;
}
}
}
}) reduceBlock:^id(NSArray *keys, NSArray *values, BOOL rereduce) {
return @(keys.count);
} version: versionNumber];
So that's how I handled it.
On Wednesday, March 23, 2016 at 9:00:53 AM UTC-6, Jens Alfke wrote:
>
>
> On Mar 23, 2016, at 4:19 AM, Brendan Duddridge <[email protected]
> <javascript:>> wrote:
>
> How can I emit unique values in my map block? Is there a way to do that
> easily or will it require keeping an array of already emitted values,
> ensuring that I don't emit another one with the same value? I just don't
> want duplicate keys in my view.
>
>
> Duplicate *values* or duplicate *keys*?
>
> If your map block just calls emit() once, the docID will be unique in the
> index. If you call emit() multiple times, you can append a different string
> to the docID in each emit() call, like “-1”, “-2”, etc.
>
> But I have a feeling I’m misunderstanding your question…
>
> —Jens
>
--
You received this message because you are subscribed to the Google Groups
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/mobile-couchbase/3c048605-a0cf-4aa0-b419-e2569de16b38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.