JSFiddle - React, Tailwind, and code Playground
by Trisox
HTML
const set = (obj, path, val) => {
const keys = path.split('.');
const lastKey = keys.pop();
const lastObj = keys.reduce((obj, key) =>
obj[key] = obj[key] || {},
obj);
lastObj[lastKey] = val;
};
Example:
const obj = {'a': {'prop': {'that': 'exists'}}};
set(obj, 'a.very.deep.prop', 'value');
console.log(JSON.stringify(obj));
// {"a":{"prop":{"that":"exists"},"very":{"deep":{"prop":"value"}}}}
JavaScript
var serverRequest = [{
"name": "date",
"configuration": {
"defaultValue": "2018-01-01T00:00:00.000Z",
"editable": true,
"visibile": true
}
}, {
"name": "numerOfPersons",
"configuration": {
"defaultValue": 1,
"editable": true,
"visibile": true
},
}, {
"naam": "verzekerde[0].tussenvoegsels",
"configuratie": {
"defaultwaarde": '',
"aanpasbaar": true,
"zichtbaar": true
}
}]
var ui = {};
var fields = {};
function setFields(name, defaultValue) {
_.set(fields, 'fields'+name, defaultValue)
}
function setUI(name, config) {
var configuration = {
...config
}
delete configuration.defaultValue;
ui[name] = configuration;
}
serverRequest.forEach(function(ObjectState) {
setFields(ObjectState.name, ObjectState.configuration.defaultValue)
//setUI(ObjectState.name, ObjectState.configuration)
});
console.log(serverRequest, 'serverRequest');
console.log(ui, 'ui');
console.log(fields, 'fields');