ODK-X: Help with queries javascript code

Hi all,

I’m using a external csv-file looking like:

region_name, region_nr, village_name, …
region1_name, 1, village11_name, …
region1_name, 1, village12_name, …
region1_name, 1, village13_name, …

region2_name, 2, village21_name, …
region2_name, 2, village22_name, …

regionN_name, N, villageN1_name, …

Is it possible to display the region_name in a select_one_dropdown, but save the region_nr in the database?

The code for pulling the data is:
_.chain(context).pluck(‘region_name’).uniq().map(function(region_name){
return {data_value:region_name, display:{title: {text: region_name} } };
}).value()

I’ve tried changing; data_value: region_nr, without any luck.

Hi Andreas,

I think the problem with your query is that you have the .pluck(‘region_name’). What that basically does is filter out anything except ‘region_name’ (i.e. you lose the rich object with e.g. the .region_nr property, and just have the string value of region_name available in your map() function…

Of course, if you leave out the pluck(‘region_name’), then your uniq() function will have no effect resulting in duplicates. :wink:

Best
/emil

Hello again,
Just in case you were wondering how to get your uniq() to work, you can feed it with a function that determines how to evaluate uniqueness, e.g.

_.chain(context)
.uniq(function(x) {
return x.region_name
})
.map(function(place){
return {
data_value:place.region_nr,
display:{title: {text: place.region_name} } };
}).value()

…you could also determine uniqueness based on x.region_nr for a slight performance benefit (assuming it is a number type).

Note also, that your return object can include more properties, in case you want to use it in a choice_filter, e.g. when you have sub-regions you might want to include the parent_region as a property in the result of the map() function…

Good luck :slight_smile:

/emil

1 Like

Hi @Emil,

Thanks a lot.
Your solution works great!