Guest User

Untitled

a guest
Feb 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. isObject = a => {
  2. if (typeof a === 'object') {
  3. return true;
  4. }
  5. return false;
  6. };
  7.  
  8. function serialize(o, target = {}, prefix = '') {
  9. Object.keys(o).forEach(key => {
  10. const value = o[key];
  11. if (isObject(value)) {
  12. serialize(value, target, prefix + key + '.');
  13. } else {
  14. target[prefix + key] = value;
  15. }
  16. });
  17.  
  18. return target;
  19. }
  20.  
  21. function deserialize(src, target = {}) {
  22. Object.keys(src).forEach(key => {
  23. const objPath = key.split('.');
  24. const propName = objPath.splice(objPath.length - 1)[0];
  25. const o = objPath.reduce((acc, pName) => acc[pName] = acc[pName] || {}, target);
  26. o[propName] = src[key];
  27. });
  28. return target;
  29. }
  30.  
  31. o1 = {
  32. title: 'title',
  33. banner: {
  34. title: 'asd'
  35. },
  36. validations: {
  37. req: '',
  38. minmax:''
  39. },
  40. placeholders:{
  41. firstName: ''
  42. }
  43. };
  44.  
  45.  
  46. deserialize(serialize(o1))
Add Comment
Please, Sign In to add comment