Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const deepSet = (obj, path, value) => {
- const elements = (Array.isArray(path) && path) || path.split('.')
- if (elements.length > 1) {
- obj[elements[0]] = ((!obj.hasOwnProperty(elements[0]) || typeof obj[elements[0]] !== 'object') && {} ) || obj[elements[0]]
- deepSet(obj[elements[0]], elements.slice(1), value)
- } else {
- obj[elements[0]] = value
- }
- return obj
- }
- const flatDataToObject = (flatData, dataModel) => {
- const rowLength = Array.isArray(flatData) && flatData[0] && Array.isArray(flatData[0]) && flatData[0].length;
- const modelLength = Array.isArray(dataModel) && dataModel.length
- return rowLength === modelLength && flatData.reduce((accRow, currRow) => [
- ...accRow, currRow.reduce((accCol, currCol, currIdx) => deepSet(accCol, dataModel[currIdx], currCol), {})
- ], [])
- }
- // Test
- const flatData = [
- ['this1', 'that1', 'the1', 'other1', 'thing1', 'thang1', 1, 'end1'],
- ['this2', 'that2', 'the2', 'other2', 'thing2', 'thang2', 2, 'end2'],
- ['this3', 'that3', 'the3', 'other3', 'thing3', 'thang3', 3, 'end3'],
- ['this4', 'that4', 'the4', 'other4', 'thing4', 'thang4', 4, 'end4'],
- ['this5', 'that5', 'the5', 'other5', 'thing5', 'thang5', 5, 'end5']
- ]
- const dataModel = [
- 'omg.this.that',
- 'what.is.this',
- 'omg.this.spoon.is.too.big',
- 'omg.this.all.there.is',
- 'omg.that.thing',
- 'omg.this.works',
- 'omg.fine.be.that.way',
- 'omg.fine.be.that.thing'
- ]
- const test = flatDataToObject(flatData, dataModel)
- console.log(JSON.stringify(test, null, 2))
- /*
- [
- {
- "omg": {
- "this": {
- "that": "this1",
- "spoon": {
- "is": {
- "too": {
- "big": "the1"
- }
- }
- },
- "all": {
- "there": {
- "is": "other1"
- }
- },
- "works": "thang1"
- },
- "that": {
- "thing": "thing1"
- },
- "fine": {
- "be": {
- "that": {
- "way": 1,
- "thing": "end1"
- }
- }
- }
- },
- "what": {
- "is": {
- "this": "that1"
- }
- }
- },
- {
- "omg": {
- "this": {
- "that": "this2",
- "spoon": {
- "is": {
- "too": {
- "big": "the2"
- }
- }
- },
- "all": {
- "there": {
- "is": "other2"
- }
- },
- "works": "thang2"
- },
- "that": {
- "thing": "thing2"
- },
- "fine": {
- "be": {
- "that": {
- "way": 2,
- "thing": "end2"
- }
- }
- }
- },
- "what": {
- "is": {
- "this": "that2"
- }
- }
- },
- {
- "omg": {
- "this": {
- "that": "this3",
- "spoon": {
- "is": {
- "too": {
- "big": "the3"
- }
- }
- },
- "all": {
- "there": {
- "is": "other3"
- }
- },
- "works": "thang3"
- },
- "that": {
- "thing": "thing3"
- },
- "fine": {
- "be": {
- "that": {
- "way": 3,
- "thing": "end3"
- }
- }
- }
- },
- "what": {
- "is": {
- "this": "that3"
- }
- }
- },
- {
- "omg": {
- "this": {
- "that": "this4",
- "spoon": {
- "is": {
- "too": {
- "big": "the4"
- }
- }
- },
- "all": {
- "there": {
- "is": "other4"
- }
- },
- "works": "thang4"
- },
- "that": {
- "thing": "thing4"
- },
- "fine": {
- "be": {
- "that": {
- "way": 4,
- "thing": "end4"
- }
- }
- }
- },
- "what": {
- "is": {
- "this": "that4"
- }
- }
- },
- {
- "omg": {
- "this": {
- "that": "this5",
- "spoon": {
- "is": {
- "too": {
- "big": "the5"
- }
- }
- },
- "all": {
- "there": {
- "is": "other5"
- }
- },
- "works": "thang5"
- },
- "that": {
- "thing": "thing5"
- },
- "fine": {
- "be": {
- "that": {
- "way": 5,
- "thing": "end5"
- }
- }
- }
- },
- "what": {
- "is": {
- "this": "that5"
- }
- }
- }
- ]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement