Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- isObject = a => {
- if (typeof a === 'object') {
- return true;
- }
- return false;
- };
- function serialize(o, target = {}, prefix = '') {
- Object.keys(o).forEach(key => {
- const value = o[key];
- if (isObject(value)) {
- serialize(value, target, prefix + key + '.');
- } else {
- target[prefix + key] = value;
- }
- });
- return target;
- }
- function deserialize(src, target = {}) {
- Object.keys(src).forEach(key => {
- const objPath = key.split('.');
- const propName = objPath.splice(objPath.length - 1)[0];
- const o = objPath.reduce((acc, pName) => acc[pName] = acc[pName] || {}, target);
- o[propName] = src[key];
- });
- return target;
- }
- o1 = {
- title: 'title',
- banner: {
- title: 'asd'
- },
- validations: {
- req: '',
- minmax:''
- },
- placeholders:{
- firstName: ''
- }
- };
- deserialize(serialize(o1))
Add Comment
Please, Sign In to add comment