logo
Tags down

shadow

convert JavaScript nested array for priming tree format


By : user2175010
Date : October 16 2020, 08:10 PM
This might help you Working Example
code :
validate(a) {
    let newArr = [];
    for (const key in a) {
      if (key) {
        newArr.push({data: key, label: key, childern: this.validate(a[key])});
      }
    }
    return newArr;
}

const a = {
        "com": {
            "ups": {
                "demo": {
                    "a": 9
                }
            }
        }
    };

 console.log(this.validate(a));


Share : facebook icon twitter icon

Convert PHP nested array to XML tree structure


By : user2293077
Date : March 29 2020, 07:55 AM
around this issue I have following array in PHP and I want to convert it equivalent XML. , I got solution like following code
code :
<?php
header("Content-type: text/xml; charset=utf-8");
$data = array(
    "Details" => array(
        "PID" => "1234","OPID" => "1345"
    ),"Parameters" => array(
        "HR" => "112","SPO2" => "0"
    )
);

$domtree = new DOMDocument('1.0', 'UTF-8');
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("VitalParameters");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);

foreach ($data as $key=>$value)
{
    $currentElement= $domtree->createElement($key);
    $currentElement= $xmlRoot->appendChild($currentElement);
    if(is_array($value))
    {
        foreach ($value as $k=>$v)
        {
            $currentElement->appendChild($domtree->createElement($k,$v));
        }
    }
}

/* get the xml printed */
echo $domtree->saveXML();
<VitalParameters>
  <Details>
    <PID>1234</PID>
    <OPID>1345</OPID>
  </Details>
  <Parameters>
    <HR>112</HR>
    <SPO2>0</SPO2>
  </Parameters>
</VitalParameters>

Convert from self-reference array into nested array in tree


By : user3387706
Date : March 29 2020, 07:55 AM
should help you out I use angular-bootstrap-nav-tree , This will do the job:
code :
function nest (array) {
  nested = [];
  for (var i = 0; i < array.length; i++) {
    var parent = array[i].parent;
    if (!parent) {
      nested.push(array[i]);
    } else {
      // You'll want to replace this with a more efficient search
      for (var j = 0; j < array.length; j++) {
        if (array[j].id === parent) {
          array[j].children = array[j].children || [];
          array[j].children.push(array[i]);
        }
      }
    }
  }
  return nested;
}

How to convert newick tree format to tree like Hierarchical javascript object


By : Ketluts
Date : March 29 2020, 07:55 AM
around this issue The code below should work correctly with any input similar to your example.
However, it assumes that each node is identified with a single character. You'd have to modify it to support longer symbols.
code :
var newick = '(A,B,(C,D)E)F',
    stack = [],
    child,
    root = [],
    node = root;

newick.split('').reverse().forEach(function(n) {
  switch(n) {
    case ')':
      // ')' => begin child node
      stack.push(node);
      node = child.children = [];
      break;

    case '(':
      // '(' => end of child node
      node = stack.pop();
      break;

    case ',':
      // ',' => separator (ignored)
      break;

    default:
      // assume all other characters are node names
      node.push(child = { name: n });
      break;
  }
});
var dmp;

(dmp = function(node, level) {
  node.forEach(function(n) {
    console.log(Array(level).join('-') + n.name);
    n.children && dmp(n.children, level + 1);
  });
})(root, 1);
F
-E
--D
--C
-B
-A

How to convert array of arrays into deep nested tree view


By : Manojkumar
Date : March 29 2020, 07:55 AM
Hope this helps You could use a nested hash table for the access for the routes and take an array as result set. If you have only one root element, you could take the first element of the result array.
code :
var routes = [['top', '1.jpg'], ['top', '2.jpg'], ['top', 'unsplash', 'photo.jpg'], ['top', 'unsplash', 'photo2.jpg'], ['top', 'foo', '2.jpg'], ['top', 'foo', 'bar', '1.jpg'], ['top', 'foo', 'bar', '2.jpg']],
    result = [],
    temp = { _: result };

routes.forEach(function (path) {
    path.reduce(function (level, key) {
        if (!level[key]) {
            level[key] = { _: [] };
            level._.push({ name: key, children: level[key]._ });
        }
        return level[key];
    }, temp);
});

console.log(result);    
.as-console-wrapper { max-height: 100% !important; top: 0; }
var routes = [['top', '1.jpg'], ['top', '2.jpg'], ['top', 'unsplash', 'photo.jpg'], ['top', 'unsplash', 'photo2.jpg'], ['top', 'foo', '2.jpg'], ['top', 'foo', 'bar', '1.jpg'], ['top', 'foo', 'bar', '2.jpg']],
    result = [];

routes.forEach(function (path) {
    path.reduce(function (level, key) {
        var temp = level.find(({ name }) => key === name);
        if (!temp) {
            temp = { name: key, children: [] };
            level.push(temp);
        }
        return temp.children;
    }, result);
});

console.log(result);    
.as-console-wrapper { max-height: 100% !important; top: 0; }

javascript convert nested dictionary to tree like array structure


By : İsmail
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I would write a second function for the final data conversion. Your items_to_tree is pretty generic and reusable. Converting this tree to the desired format can be done in a few lines:
code :
function tree_to_format(tree) {
  return Object
    .keys(tree)
    .map(k => ({ name: k, children: tree_to_format(tree[k]) }))
};
function items_to_tree(items) {
    var arr = {};
    items.forEach(function(item){
        var parts = item.split(".");
        var last = parts.pop();
        var cursor = arr;
        parts.forEach(function(part){
            if(!cursor[part]) cursor[part] = {};
            cursor = cursor[part];
        });
        cursor[last] = {};
    });
    return arr;
}

function tree_to_format(tree) {
  return Object
    .keys(tree)
    .map(k => ({ name: k, children: tree_to_format(tree[k]) }))
};

console.log(
  tree_to_format(
    items_to_tree(["a.b", "a.c", "b.c", "a.c", "a.c.d", "a.b.d"])
  )
);

// or even:
const compose = (f, g) => x => f(g(x));
const items_to_format = compose(tree_to_format, items_to_tree);
shadow
Privacy Policy - Terms - Contact Us © voile276.org