Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. const deepSet = (obj, path, value) => {
  2. const elements = (Array.isArray(path) && path) || path.split('.')
  3.  
  4. if (elements.length > 1) {
  5. obj[elements[0]] = ((!obj.hasOwnProperty(elements[0]) || typeof obj[elements[0]] !== 'object') && {} ) || obj[elements[0]]
  6. deepSet(obj[elements[0]], elements.slice(1), value)
  7. } else {
  8. obj[elements[0]] = value
  9. }
  10.  
  11. return obj
  12. }
  13.  
  14. const flatDataToObject = (flatData, dataModel) => {
  15. const rowLength = Array.isArray(flatData) && flatData[0] && Array.isArray(flatData[0]) && flatData[0].length;
  16. const modelLength = Array.isArray(dataModel) && dataModel.length
  17.  
  18. return rowLength === modelLength && flatData.reduce((accRow, currRow) => [
  19. ...accRow, currRow.reduce((accCol, currCol, currIdx) => deepSet(accCol, dataModel[currIdx], currCol), {})
  20. ], [])
  21. }
  22.  
  23. // Test
  24. const flatData = [
  25. ['this1', 'that1', 'the1', 'other1', 'thing1', 'thang1', 1, 'end1'],
  26. ['this2', 'that2', 'the2', 'other2', 'thing2', 'thang2', 2, 'end2'],
  27. ['this3', 'that3', 'the3', 'other3', 'thing3', 'thang3', 3, 'end3'],
  28. ['this4', 'that4', 'the4', 'other4', 'thing4', 'thang4', 4, 'end4'],
  29. ['this5', 'that5', 'the5', 'other5', 'thing5', 'thang5', 5, 'end5']
  30. ]
  31.  
  32. const dataModel = [
  33. 'omg.this.that',
  34. 'what.is.this',
  35. 'omg.this.spoon.is.too.big',
  36. 'omg.this.all.there.is',
  37. 'omg.that.thing',
  38. 'omg.this.works',
  39. 'omg.fine.be.that.way',
  40. 'omg.fine.be.that.thing'
  41. ]
  42.  
  43. const test = flatDataToObject(flatData, dataModel)
  44. console.log(JSON.stringify(test, null, 2))
  45.  
  46. /*
  47. [
  48. {
  49. "omg": {
  50. "this": {
  51. "that": "this1",
  52. "spoon": {
  53. "is": {
  54. "too": {
  55. "big": "the1"
  56. }
  57. }
  58. },
  59. "all": {
  60. "there": {
  61. "is": "other1"
  62. }
  63. },
  64. "works": "thang1"
  65. },
  66. "that": {
  67. "thing": "thing1"
  68. },
  69. "fine": {
  70. "be": {
  71. "that": {
  72. "way": 1,
  73. "thing": "end1"
  74. }
  75. }
  76. }
  77. },
  78. "what": {
  79. "is": {
  80. "this": "that1"
  81. }
  82. }
  83. },
  84. {
  85. "omg": {
  86. "this": {
  87. "that": "this2",
  88. "spoon": {
  89. "is": {
  90. "too": {
  91. "big": "the2"
  92. }
  93. }
  94. },
  95. "all": {
  96. "there": {
  97. "is": "other2"
  98. }
  99. },
  100. "works": "thang2"
  101. },
  102. "that": {
  103. "thing": "thing2"
  104. },
  105. "fine": {
  106. "be": {
  107. "that": {
  108. "way": 2,
  109. "thing": "end2"
  110. }
  111. }
  112. }
  113. },
  114. "what": {
  115. "is": {
  116. "this": "that2"
  117. }
  118. }
  119. },
  120. {
  121. "omg": {
  122. "this": {
  123. "that": "this3",
  124. "spoon": {
  125. "is": {
  126. "too": {
  127. "big": "the3"
  128. }
  129. }
  130. },
  131. "all": {
  132. "there": {
  133. "is": "other3"
  134. }
  135. },
  136. "works": "thang3"
  137. },
  138. "that": {
  139. "thing": "thing3"
  140. },
  141. "fine": {
  142. "be": {
  143. "that": {
  144. "way": 3,
  145. "thing": "end3"
  146. }
  147. }
  148. }
  149. },
  150. "what": {
  151. "is": {
  152. "this": "that3"
  153. }
  154. }
  155. },
  156. {
  157. "omg": {
  158. "this": {
  159. "that": "this4",
  160. "spoon": {
  161. "is": {
  162. "too": {
  163. "big": "the4"
  164. }
  165. }
  166. },
  167. "all": {
  168. "there": {
  169. "is": "other4"
  170. }
  171. },
  172. "works": "thang4"
  173. },
  174. "that": {
  175. "thing": "thing4"
  176. },
  177. "fine": {
  178. "be": {
  179. "that": {
  180. "way": 4,
  181. "thing": "end4"
  182. }
  183. }
  184. }
  185. },
  186. "what": {
  187. "is": {
  188. "this": "that4"
  189. }
  190. }
  191. },
  192. {
  193. "omg": {
  194. "this": {
  195. "that": "this5",
  196. "spoon": {
  197. "is": {
  198. "too": {
  199. "big": "the5"
  200. }
  201. }
  202. },
  203. "all": {
  204. "there": {
  205. "is": "other5"
  206. }
  207. },
  208. "works": "thang5"
  209. },
  210. "that": {
  211. "thing": "thing5"
  212. },
  213. "fine": {
  214. "be": {
  215. "that": {
  216. "way": 5,
  217. "thing": "end5"
  218. }
  219. }
  220. }
  221. },
  222. "what": {
  223. "is": {
  224. "this": "that5"
  225. }
  226. }
  227. }
  228. ]
  229. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement