JSFiddle - React, Tailwind, and code Playground
CSS
body {
font-family: monospace;
white-space: pre-wrap;
}
JavaScript
$(document).ready(function () {
Facets = [{
"term": "Time",
"Time": [{
"term": "Microsoft",
"Microsoft": [{
"count": 1,
"term": "MSFT"
}, {
"count": 1,
"term": "Microsoft"
}]
}, {
"Intel_Corp": [{
"count": 1,
"term": "(INTC, Fortune 500)"
}],
"term": "Intel Corp",
}]
}, {
"term": "News",
"News": [{
"term": "Germany",
"Germany": [{
"count": 2,
"term": "Germany"
}]
}, {
"term": "Japan",
"Japan": [{
"count": 1,
"term": "Japan"
}],
}]
}, {
"OnelevelTestData": [{
"count": 2,
"term": "Bernanke"
}, {
"count": 1,
"term": "Fed"
}, {
"count": 2,
"term": "Federal Reserve"
}],
"term": "OnelevelTestData"
}];
$("body").text(JSON.stringify(generateList(Facets), undefined, 4));
});
function generateList(facets) {
var map = {};
(function recurse(arr) {
var join = [];
for (var i=0; i<arr.length; i++) {
var current = arr[i].term; // every object must have one!
current = current.replace(/ /g, "_")
join.push(current); // only on lowest level?
if (current in arr[i])
map[current] = recurse(arr[i][current]);
}
return join;
})(facets)
return map;
}