From 8a586fc32a9684a4931bbc55abb6ad3ae8a40cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Egon=20Rich=C3=A1rd=20T=C5=91r=C3=B6s?= Date: Mon, 12 Mar 2018 21:42:13 +0100 Subject: [PATCH] :sparkles: Ajax - fetch properties from object --- src/scripts/src/choices.js | 5 +++-- src/scripts/src/lib/utils.js | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/scripts/src/choices.js b/src/scripts/src/choices.js index 51c3658..35eaecd 100644 --- a/src/scripts/src/choices.js +++ b/src/scripts/src/choices.js @@ -25,6 +25,7 @@ import { generateId, findAncestorByAttrName, regexFilter, + fetchFromObject, } from './lib/utils'; import './lib/polyfills'; @@ -1177,8 +1178,8 @@ class Choices { ); } else { this._addChoice( - result[value], - result[label], + fetchFromObject(result, value), + fetchFromObject(result, label), result.selected, result.disabled, undefined, diff --git a/src/scripts/src/lib/utils.js b/src/scripts/src/lib/utils.js index af1833d..1312601 100644 --- a/src/scripts/src/lib/utils.js +++ b/src/scripts/src/lib/utils.js @@ -602,4 +602,19 @@ export const reduceToValues = (items, key = 'value') => { }, []); return values; -} \ No newline at end of file +} + +/** + * Fetch properties from object + * @param {Object} object Related object + * @param {String} properties Properties from object + */ +export const fetchFromObject = function (object, properties){ + const index = properties.indexOf('.'); + + if(index > -1){ + return fetchFromObject(object[properties.substring(0, index)], properties.substr(index + 1)); + } + + return object[properties]; +}; \ No newline at end of file