From ac059c7431c3c1e2bf7a7b1415a2da1067d0d6c6 Mon Sep 17 00:00:00 2001 From: Omar De La Hoz Date: Wed, 21 Mar 2018 17:17:23 -0400 Subject: [PATCH] Added support to nested objects. --- index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index add82d6..41eef0d 100755 --- a/index.js +++ b/index.js @@ -50,7 +50,7 @@ let buildExport = params => { dataset.forEach(record => { let row = [] for (let col in specification) { - let cell_value = record[col] + let cell_value = getValue(col.split('.'), record); if (specification[col].cellFormat && typeof specification[col].cellFormat == 'function') { cell_value = specification[col].cellFormat(record[col], record) @@ -85,6 +85,23 @@ let buildExport = params => { } +/** + * Gets nested values in nested objects. + * @param {String} path Path of value. + * @param {Object} obj Object to get the value from. + * @return Value for specified path. + */ +function getValue(path, obj){ + if(path.length === 1){ + + return obj[path[0]]; + } + else{ + + return getValue(path, obj[path.shift()]); + } +} + module.exports = { buildExport }