jQuery addClass example
Change class name on click in jQuery
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<body>
<div id="output"></div>
</body>
JavaScript
var data = [[1,2,3,4,5],
[1,2,6,4,5],
[1,3,6,4,5],
[1,2,3,6,5],
[1,7,5],
[1,7,3,5]]
var root = {"name":"", "children":[]} // the output
var node // pointer thingy
var row
for(var i=0;i<data.length;i++){
row = data[i];
node = root; // for each row start at the beginning?
for(var j=0;j<row.length;j++){
if (typeof row[j] !== 'undefined' && row[j] !== null) {
attribute = row[j]
}else{
attribute = "null"
}
if(_.where(node.children, {name:attribute}) == false ){
// qq(attribute+" not found")
if(j < row.length -1){
var oobj = {"name":attribute, "children":[] }
node.children.push(oobj)
node = node.children[node.children.length-1]
}else{
// last attribute, set to value1, or increment
node.children.push({"name":attribute, "size":1 })
}
}else{
found = false
pos = 0
// we should always find the position, as the above If confirmed it
for(var k=0;k< node.children.length ;k++){
if(node.children[k]['name'] == attribute){
pos = k
found = true
}
}
if(found == false){
ww(0, "data to flare: child "+attribute+" found but not found")
}
if(!node.children[pos]['children']){
node.children[pos]['size'] = node.children[pos]['size']+ 1
}else{
node = node.children[node.children.length-1]
}
}
}
}
// object here
console.log(root)
// stringified version to page
document.getElementById('output').innerHTML = JSON.stringify(root);