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, generateId,
findAncestorByAttrName, findAncestorByAttrName,
regexFilter, regexFilter,
fetchFromObject,
} from './lib/utils'; } from './lib/utils';
import './lib/polyfills'; import './lib/polyfills';
@ -1177,8 +1178,8 @@ class Choices {
); );
} else { } else {
this._addChoice( this._addChoice(
result[value], fetchFromObject(result, value),
result[label], fetchFromObject(result, label),
result.selected, result.selected,
result.disabled, result.disabled,
undefined, undefined,

View file

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