Ajax - fetch properties from object

This commit is contained in:
Egon Richárd Tőrös 2018-03-12 21:42:13 +01:00
parent b49980d169
commit 8a586fc32a
2 changed files with 19 additions and 3 deletions

View file

@ -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,

View file

@ -602,4 +602,19 @@ export const reduceToValues = (items, key = 'value') => {
}, []);
return values;
}
}
/**
* 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];
};