@talend/react-components
Version:
Set of react components.
29 lines (28 loc) • 851 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = toFlat;
/**
* take an object and create a new FLAT object
* { foo: { bar: 'baz' } } -> { "$['foo']['bar']": 'baz' }
* @param {any} data [description]
* @param {Object} buffer because it's recursive
* @param {String} path to keep the json path
* @return {Object} flatten
*/
function toFlat(data, buffer = {}, path = '$') {
if (Array.isArray(data)) {
data.forEach((value, index) => {
toFlat(value, buffer, `${path}[${index}]`);
});
} else if (!!data && typeof data === 'object') {
Object.keys(data).forEach(key => {
toFlat(data[key], buffer, `${path}['${key}']`);
});
} else {
buffer[path] = data; // eslint-disable-line no-param-reassign
}
return buffer;
}
//# sourceMappingURL=toflat.js.map