From d12a5e7cf29f42ec8ec3da36bd8fa8f66fdfd4ea Mon Sep 17 00:00:00 2001 From: jos Date: Sat, 4 Jan 2020 12:24:12 +0100 Subject: [PATCH] Improve projection in example to flatten picking a single field --- examples/23_custom_query_language.html | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/23_custom_query_language.html b/examples/23_custom_query_language.html index c6e8d43..33c21e2 100644 --- a/examples/23_custom_query_language.html +++ b/examples/23_custom_query_language.html @@ -74,12 +74,16 @@ if (projection) { // It is possible to make a util function "pickFlat" // and use that when building the query to make it more readable. - const fields = projection.fields.map(field => { - const name = _.last(field.split('.')) - return ` '${name}': _.get(item, '${field}')` - }) - - query = `_.map(${query}, item => ({\n${fields.join(',\n')}})\n)` + if (projection.fields.length > 1) { + const fields = projection.fields.map(field => { + const name = _.last(field.split('.')) + return ` '${name}': _.get(item, '${field}')` + }) + query = `_.map(${query}, item => ({\n${fields.join(',\n')}})\n)` + } else { + const field = projection.fields[0] + query = `_.map(${query}, item => _.get(item, '${field}'))` + } } return query