How to convert Data table to tree json format dynamically?
I'm using this data input to create a D3.js visualization:
Tree Layout
Okay so the right now my data input is a json file which i have hardcoded:
"name":"Fun",
"children": [
"name": "Legal",
"children": [
"name": "Adventure" ,
"name": "Movie" ,
"name": "M&m"
]
,
"name": "frowned upon",
"children": [
"name": "recreational stuff" ,
"name": "religious views"
]
]
But my data input is actually :
How do i convert this data table dynamically to the above mentioned Json format, do i write a function which parses the data table to convert it into the above format or this is there another way.
javascript json d3.js data-visualization
add a comment |
I'm using this data input to create a D3.js visualization:
Tree Layout
Okay so the right now my data input is a json file which i have hardcoded:
"name":"Fun",
"children": [
"name": "Legal",
"children": [
"name": "Adventure" ,
"name": "Movie" ,
"name": "M&m"
]
,
"name": "frowned upon",
"children": [
"name": "recreational stuff" ,
"name": "religious views"
]
]
But my data input is actually :
How do i convert this data table dynamically to the above mentioned Json format, do i write a function which parses the data table to convert it into the above format or this is there another way.
javascript json d3.js data-visualization
add a comment |
I'm using this data input to create a D3.js visualization:
Tree Layout
Okay so the right now my data input is a json file which i have hardcoded:
"name":"Fun",
"children": [
"name": "Legal",
"children": [
"name": "Adventure" ,
"name": "Movie" ,
"name": "M&m"
]
,
"name": "frowned upon",
"children": [
"name": "recreational stuff" ,
"name": "religious views"
]
]
But my data input is actually :
How do i convert this data table dynamically to the above mentioned Json format, do i write a function which parses the data table to convert it into the above format or this is there another way.
javascript json d3.js data-visualization
I'm using this data input to create a D3.js visualization:
Tree Layout
Okay so the right now my data input is a json file which i have hardcoded:
"name":"Fun",
"children": [
"name": "Legal",
"children": [
"name": "Adventure" ,
"name": "Movie" ,
"name": "M&m"
]
,
"name": "frowned upon",
"children": [
"name": "recreational stuff" ,
"name": "religious views"
]
]
But my data input is actually :
How do i convert this data table dynamically to the above mentioned Json format, do i write a function which parses the data table to convert it into the above format or this is there another way.
javascript json d3.js data-visualization
javascript json d3.js data-visualization
edited Nov 15 '18 at 6:13
Foo
1
1
asked Nov 15 '18 at 6:07
Shricharan ArumugamShricharan Arumugam
457
457
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
var test = new function()
var table = [
['fun', 'legal', 'adventure'],
['fun', 'legal', 'mvie'],
['fun', 'legal', 'M&M'],
['fun', 'Frowned upon', 'Rec stuff'],
['fun', 'Frowned upon', 'Regligious views']
];
var res = ;
this.init = function()
for (var i = 0; i < table.length; i++)
var curRow = table[i];
test.myAddFun(res, curRow, 0);
console.log(res);
;
this.myAddFun = function(_res, arr, startIndex)
var addedToJSON = false;
for (var i = 0; i < _res.length; i++)
var curJSON = _res[i];
if (curJSON['name'] == arr[startIndex])
if (startIndex < arr.length - 1)
test.myAddFun(curJSON['children'], arr, startIndex + 1);
addedToJSON = true;
break;
if (addedToJSON)
return;
var curJSON = ;
curJSON['name'] = arr[startIndex];
if (startIndex < arr.length - 1)
curJSON['children'] = ;
test.myAddFun(curJSON['children'], arr, startIndex + 1);
_res.push(curJSON);
return;
;
;
test.init();
add a comment |
You can write recursive function to restructure data.
const a = [[
'fun', 'legal', 'adventure',
], [
'fun', 'legal', 'movie',
], [
'fun', 'legal', 'm&m'
], [
'fun', 'frowned upon', 'rec stuff'
], [
'fun', 'frowned upon', 'religius views'
]];
let t = ;
const addLeaf = (array) => ;
const addToTree = (tree, array) =>
if (!array ;
a.forEach((entry) =>
t = addToTree(t, entry);
);
console.log(JSON.stringify(t, null, 2))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313396%2fhow-to-convert-data-table-to-tree-json-format-dynamically%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
var test = new function()
var table = [
['fun', 'legal', 'adventure'],
['fun', 'legal', 'mvie'],
['fun', 'legal', 'M&M'],
['fun', 'Frowned upon', 'Rec stuff'],
['fun', 'Frowned upon', 'Regligious views']
];
var res = ;
this.init = function()
for (var i = 0; i < table.length; i++)
var curRow = table[i];
test.myAddFun(res, curRow, 0);
console.log(res);
;
this.myAddFun = function(_res, arr, startIndex)
var addedToJSON = false;
for (var i = 0; i < _res.length; i++)
var curJSON = _res[i];
if (curJSON['name'] == arr[startIndex])
if (startIndex < arr.length - 1)
test.myAddFun(curJSON['children'], arr, startIndex + 1);
addedToJSON = true;
break;
if (addedToJSON)
return;
var curJSON = ;
curJSON['name'] = arr[startIndex];
if (startIndex < arr.length - 1)
curJSON['children'] = ;
test.myAddFun(curJSON['children'], arr, startIndex + 1);
_res.push(curJSON);
return;
;
;
test.init();
add a comment |
var test = new function()
var table = [
['fun', 'legal', 'adventure'],
['fun', 'legal', 'mvie'],
['fun', 'legal', 'M&M'],
['fun', 'Frowned upon', 'Rec stuff'],
['fun', 'Frowned upon', 'Regligious views']
];
var res = ;
this.init = function()
for (var i = 0; i < table.length; i++)
var curRow = table[i];
test.myAddFun(res, curRow, 0);
console.log(res);
;
this.myAddFun = function(_res, arr, startIndex)
var addedToJSON = false;
for (var i = 0; i < _res.length; i++)
var curJSON = _res[i];
if (curJSON['name'] == arr[startIndex])
if (startIndex < arr.length - 1)
test.myAddFun(curJSON['children'], arr, startIndex + 1);
addedToJSON = true;
break;
if (addedToJSON)
return;
var curJSON = ;
curJSON['name'] = arr[startIndex];
if (startIndex < arr.length - 1)
curJSON['children'] = ;
test.myAddFun(curJSON['children'], arr, startIndex + 1);
_res.push(curJSON);
return;
;
;
test.init();
add a comment |
var test = new function()
var table = [
['fun', 'legal', 'adventure'],
['fun', 'legal', 'mvie'],
['fun', 'legal', 'M&M'],
['fun', 'Frowned upon', 'Rec stuff'],
['fun', 'Frowned upon', 'Regligious views']
];
var res = ;
this.init = function()
for (var i = 0; i < table.length; i++)
var curRow = table[i];
test.myAddFun(res, curRow, 0);
console.log(res);
;
this.myAddFun = function(_res, arr, startIndex)
var addedToJSON = false;
for (var i = 0; i < _res.length; i++)
var curJSON = _res[i];
if (curJSON['name'] == arr[startIndex])
if (startIndex < arr.length - 1)
test.myAddFun(curJSON['children'], arr, startIndex + 1);
addedToJSON = true;
break;
if (addedToJSON)
return;
var curJSON = ;
curJSON['name'] = arr[startIndex];
if (startIndex < arr.length - 1)
curJSON['children'] = ;
test.myAddFun(curJSON['children'], arr, startIndex + 1);
_res.push(curJSON);
return;
;
;
test.init();
var test = new function()
var table = [
['fun', 'legal', 'adventure'],
['fun', 'legal', 'mvie'],
['fun', 'legal', 'M&M'],
['fun', 'Frowned upon', 'Rec stuff'],
['fun', 'Frowned upon', 'Regligious views']
];
var res = ;
this.init = function()
for (var i = 0; i < table.length; i++)
var curRow = table[i];
test.myAddFun(res, curRow, 0);
console.log(res);
;
this.myAddFun = function(_res, arr, startIndex)
var addedToJSON = false;
for (var i = 0; i < _res.length; i++)
var curJSON = _res[i];
if (curJSON['name'] == arr[startIndex])
if (startIndex < arr.length - 1)
test.myAddFun(curJSON['children'], arr, startIndex + 1);
addedToJSON = true;
break;
if (addedToJSON)
return;
var curJSON = ;
curJSON['name'] = arr[startIndex];
if (startIndex < arr.length - 1)
curJSON['children'] = ;
test.myAddFun(curJSON['children'], arr, startIndex + 1);
_res.push(curJSON);
return;
;
;
test.init();
var test = new function()
var table = [
['fun', 'legal', 'adventure'],
['fun', 'legal', 'mvie'],
['fun', 'legal', 'M&M'],
['fun', 'Frowned upon', 'Rec stuff'],
['fun', 'Frowned upon', 'Regligious views']
];
var res = ;
this.init = function()
for (var i = 0; i < table.length; i++)
var curRow = table[i];
test.myAddFun(res, curRow, 0);
console.log(res);
;
this.myAddFun = function(_res, arr, startIndex)
var addedToJSON = false;
for (var i = 0; i < _res.length; i++)
var curJSON = _res[i];
if (curJSON['name'] == arr[startIndex])
if (startIndex < arr.length - 1)
test.myAddFun(curJSON['children'], arr, startIndex + 1);
addedToJSON = true;
break;
if (addedToJSON)
return;
var curJSON = ;
curJSON['name'] = arr[startIndex];
if (startIndex < arr.length - 1)
curJSON['children'] = ;
test.myAddFun(curJSON['children'], arr, startIndex + 1);
_res.push(curJSON);
return;
;
;
test.init();
var test = new function()
var table = [
['fun', 'legal', 'adventure'],
['fun', 'legal', 'mvie'],
['fun', 'legal', 'M&M'],
['fun', 'Frowned upon', 'Rec stuff'],
['fun', 'Frowned upon', 'Regligious views']
];
var res = ;
this.init = function()
for (var i = 0; i < table.length; i++)
var curRow = table[i];
test.myAddFun(res, curRow, 0);
console.log(res);
;
this.myAddFun = function(_res, arr, startIndex)
var addedToJSON = false;
for (var i = 0; i < _res.length; i++)
var curJSON = _res[i];
if (curJSON['name'] == arr[startIndex])
if (startIndex < arr.length - 1)
test.myAddFun(curJSON['children'], arr, startIndex + 1);
addedToJSON = true;
break;
if (addedToJSON)
return;
var curJSON = ;
curJSON['name'] = arr[startIndex];
if (startIndex < arr.length - 1)
curJSON['children'] = ;
test.myAddFun(curJSON['children'], arr, startIndex + 1);
_res.push(curJSON);
return;
;
;
test.init();
answered Nov 15 '18 at 9:08
AbiAbi
11018
11018
add a comment |
add a comment |
You can write recursive function to restructure data.
const a = [[
'fun', 'legal', 'adventure',
], [
'fun', 'legal', 'movie',
], [
'fun', 'legal', 'm&m'
], [
'fun', 'frowned upon', 'rec stuff'
], [
'fun', 'frowned upon', 'religius views'
]];
let t = ;
const addLeaf = (array) => ;
const addToTree = (tree, array) =>
if (!array ;
a.forEach((entry) =>
t = addToTree(t, entry);
);
console.log(JSON.stringify(t, null, 2))
add a comment |
You can write recursive function to restructure data.
const a = [[
'fun', 'legal', 'adventure',
], [
'fun', 'legal', 'movie',
], [
'fun', 'legal', 'm&m'
], [
'fun', 'frowned upon', 'rec stuff'
], [
'fun', 'frowned upon', 'religius views'
]];
let t = ;
const addLeaf = (array) => ;
const addToTree = (tree, array) =>
if (!array ;
a.forEach((entry) =>
t = addToTree(t, entry);
);
console.log(JSON.stringify(t, null, 2))
add a comment |
You can write recursive function to restructure data.
const a = [[
'fun', 'legal', 'adventure',
], [
'fun', 'legal', 'movie',
], [
'fun', 'legal', 'm&m'
], [
'fun', 'frowned upon', 'rec stuff'
], [
'fun', 'frowned upon', 'religius views'
]];
let t = ;
const addLeaf = (array) => ;
const addToTree = (tree, array) =>
if (!array ;
a.forEach((entry) =>
t = addToTree(t, entry);
);
console.log(JSON.stringify(t, null, 2))
You can write recursive function to restructure data.
const a = [[
'fun', 'legal', 'adventure',
], [
'fun', 'legal', 'movie',
], [
'fun', 'legal', 'm&m'
], [
'fun', 'frowned upon', 'rec stuff'
], [
'fun', 'frowned upon', 'religius views'
]];
let t = ;
const addLeaf = (array) => ;
const addToTree = (tree, array) =>
if (!array ;
a.forEach((entry) =>
t = addToTree(t, entry);
);
console.log(JSON.stringify(t, null, 2))
const a = [[
'fun', 'legal', 'adventure',
], [
'fun', 'legal', 'movie',
], [
'fun', 'legal', 'm&m'
], [
'fun', 'frowned upon', 'rec stuff'
], [
'fun', 'frowned upon', 'religius views'
]];
let t = ;
const addLeaf = (array) => ;
const addToTree = (tree, array) =>
if (!array ;
a.forEach((entry) =>
t = addToTree(t, entry);
);
console.log(JSON.stringify(t, null, 2))
const a = [[
'fun', 'legal', 'adventure',
], [
'fun', 'legal', 'movie',
], [
'fun', 'legal', 'm&m'
], [
'fun', 'frowned upon', 'rec stuff'
], [
'fun', 'frowned upon', 'religius views'
]];
let t = ;
const addLeaf = (array) => ;
const addToTree = (tree, array) =>
if (!array ;
a.forEach((entry) =>
t = addToTree(t, entry);
);
console.log(JSON.stringify(t, null, 2))
answered Nov 15 '18 at 8:10
Delgee BDelgee B
16616
16616
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313396%2fhow-to-convert-data-table-to-tree-json-format-dynamically%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown