解析非嵌套的JSON数组为HTML树视图?

huangapple go评论127阅读模式
英文:

Parse non-nested JSON array into HTML Tree View?

问题

以下是您提供的JSON字符串的翻译部分:

[{ "Building": "18A", "Room": 219, "Location": "E-4-6", "BarCode": "P000019152" },
 { "Building": "18G", "Room": "112HALL", "Location": "J-8-5", "BarCode": "P000031111" },
 { "Building": "18G", "Room": "112HALL", "Location": "J-8-5", "BarCode": "P166279435" },
 { "Building": "18G", "Room": "112HALL", "Location": "A-10-7", "BarCode": "P352831849" },
 { "Building": "18G", "Room": "5P04", "Location": "C-4-10", "BarCode": "P726526379" },
 { "Building": "18C", "Room": "6THST", "Location": "CAGE14", "BarCode": "P000453262" },
 { "Building": "18C", "Room": "6THST", "Location": "CAGE13", "BarCode": "P954732810" }]

请注意,我只提供了JSON的翻译部分,没有包括JavaScript代码部分。如果您需要翻译代码部分,请提供具体的代码行或描述。

英文:

I am retrieving data via JSON from a database, so the data is subject to change constantly; I can't hard-code the JSON. Here is some sample JSON to illustrate this example:

[{"Building":"18A","Room":219,"Location":"E-4-6","BarCode":"P000019152"}, 
 {"Building":"18G","Room":"112HALL","Location":"J-8-5","BarCode":"P000031111"}, 
 {"Building":"18G","Room":"112HALL","Location":"J-8-5","BarCode":"P166279435"}, 
 {"Building":"18G","Room":"112HALL","Location":"A-10-7","BarCode":"P352831849"}, 
 {"Building":"18G","Room":"5P04","Location":"C-4-10","BarCode":"P726526379"}, 
 {"Building":"18C","Room":"6THST","Location":"CAGE14","BarCode":"P000453262"}, 
 {"Building":"18C","Room":"6THST","Location":"CAGE13","BarCode":"P954732810"}]

What I need to do is parse this array and create a tree view out of it. From above, the tree view would look like this:

>18A
  >219
    >E-4-6
       >P000019152
>18G
  >112HALL
    >J-8-5
       >P000019152
       >P166279435
    >A-10-7
       >P166279435
  >5P04
    >C-4-10
       >P726526379
>18C
  >6THST
     >CAGE14
        >P000453262
     >CAGE13
        >P954732810

The important thing to note is my JSON is not structured in a way that there are parent and child objects; in other words, it is not nested. Is there any way I can parse this JSON string and create a logical and hierarchical tree view out of it, based on the order of building, room, location, then bar code?

What I've tried so far:

 var input=[{"Building":"18A","Room":"219","Location":"E-4-6","BarCode":"P000019152"}, 
 {"Building":"18G","Room":"112HALL","Location":"J-8-5","BarCode":"P000031111"}, 
 {"Building":"18G","Room":"112HALL","Location":"J-8-5","BarCode":"P166279435"}, 
 {"Building":"18G","Room":"112HALL","Location":"A-10-7","BarCode":"P352831849"}, 
 {"Building":"18G","Room":"5P04","Location":"C-4-10","BarCode":"P726526379"}, 
 {"Building":"18C","Room":"6THST","Location":"CAGE14","BarCode":"P000453262"},
 {"Building":"18C","Room":"6THST","Location":"C1","BarCode":"P954732810"}];

function ToNestedObject(input){
    var i, y, len = input.length, parts, partsLen, obj = {}, prev;
    for(i = 0; i < len; i++){
        parts = input[i]; 
        partsLen = parts.length;
        prev = obj; 
        for(y = 0; y < partsLen; y++){
            prev[parts[y]] = prev[parts[y]] || {};    
            prev = prev[parts[y]];
        }
        if(!prev.ids){
            prev.ids = []; 
        }
        prev.ids.push(input[i].id); 
    }
    return obj; 
}

function ToHTML(input){
    var html = '<ul>'; 

    for(var key in input){
        if(input[key] instanceof Array){
            for(var i = 0; i < input[key].length; i++){
                html += '<li>' + input[key][i] + '</li>';
            }
        } else {
            html += '<li>' + key + ToHTML(input[key]) + '</li>';
        }           
    }
    html += '</ul>';
    return html; 
}
document.getElementById('test').innerHTML = ToHTML(ToNestedObject(input)); 

答案1

得分: 3

以下是您要的翻译内容:

您可以首先使用递归创建嵌套结构,然后使用 ulli 元素创建HTML。

const data = [
  {"Building": "18A", "Room": 219, "Location": "E-4-6", "BarCode": "P000019152"},
  {"Building": "18G", "Room": "112HALL", "Location": "J-8-5", "BarCode": "P000031111"},
  {"Building": "18G", "Room": "112HALL", "Location": "J-8-5", "BarCode": "P166279435"},
  {"Building": "18G", "Room": "112HALL", "Location": "A-10-7", "BarCode": "P352831849"},
  {"Building": "18G", "Room": "5P04", "Location": "C-4-10", "BarCode": "P726526379"},
  {"Building": "18C", "Room": "6THST", "Location": "CAGE14", "BarCode": "P000453262"},
  {"Building": "18C", "Room": "6THST", "Location": "CAGE13", "BarCode": "P954732810"}
];

const order = ['Building', 'Room', 'Location', 'BarCode'];

function nest(data, keys) {
  return data.reduce((result, e) => {
    keys.reduce((r, k, a, i) => {
      return r[e[k]] = (r[e[k]] || {})
    }, result)

    return result;
  }, {})
}

function toHtml(data, parent) {
  const ul = document.createElement('ul');
  Object.keys(data).forEach(key => {
    const li = document.createElement('li');
    li.textContent = key;
    li.appendChild(toHtml(data[key], li))
    ul.appendChild(li)
  })
  return ul;
}

const result = nest(data, order);
const html = toHtml(result);

document.body.appendChild(html)

希望这对您有帮助。如果您需要进一步的帮助,请告诉我。

英文:

You could first create nested structure using recursion and the from that data create html with ul and li elements.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = [{&quot;Building&quot;:&quot;18A&quot;,&quot;Room&quot;:219,&quot;Location&quot;:&quot;E-4-6&quot;,&quot;BarCode&quot;:&quot;P000019152&quot;}, 
{&quot;Building&quot;:&quot;18G&quot;,&quot;Room&quot;:&quot;112HALL&quot;,&quot;Location&quot;:&quot;J-8-5&quot;,&quot;BarCode&quot;:&quot;P000031111&quot;}, 
{&quot;Building&quot;:&quot;18G&quot;,&quot;Room&quot;:&quot;112HALL&quot;,&quot;Location&quot;:&quot;J-8-5&quot;,&quot;BarCode&quot;:&quot;P166279435&quot;}, 
{&quot;Building&quot;:&quot;18G&quot;,&quot;Room&quot;:&quot;112HALL&quot;,&quot;Location&quot;:&quot;A-10-7&quot;,&quot;BarCode&quot;:&quot;P352831849&quot;}, 
{&quot;Building&quot;:&quot;18G&quot;,&quot;Room&quot;:&quot;5P04&quot;,&quot;Location&quot;:&quot;C-4-10&quot;,&quot;BarCode&quot;:&quot;P726526379&quot;}, 
{&quot;Building&quot;:&quot;18C&quot;,&quot;Room&quot;:&quot;6THST&quot;,&quot;Location&quot;:&quot;CAGE14&quot;,&quot;BarCode&quot;:&quot;P000453262&quot;}, 
{&quot;Building&quot;:&quot;18C&quot;,&quot;Room&quot;:&quot;6THST&quot;,&quot;Location&quot;:&quot;CAGE13&quot;,&quot;BarCode&quot;:&quot;P954732810&quot;}]
const order = [&#39;Building&#39;, &#39;Room&#39;, &#39;Location&#39;, &#39;BarCode&#39;];
function nest(data, keys) {
return data.reduce((result, e) =&gt; {
keys.reduce((r, k, a, i) =&gt; {
return r[e[k]] = (r[e[k]] || {})
}, result)
return result;
}, {})
}
function toHtml(data, parent) {
const ul = document.createElement(&#39;ul&#39;);
Object.keys(data).forEach(key =&gt; {
const li = document.createElement(&#39;li&#39;);
li.textContent = key;
li.appendChild(toHtml(data[key], li))
ul.appendChild(li)
})
return ul;
}
const result = nest(data, order);
const html = toHtml(result);
document.body.appendChild(html)

<!-- end snippet -->

答案2

得分: 1

以下是翻译好的部分:

要开始工作,您可以遍历数组和键的层次结构,将对象数组转换为嵌套对象层次结构。这是一个一行代码的示例:

let values = [
  {
    "Building": "18A",
    "Room": 219,
    "Location": "E-4-6",
    "BarCode": "P000019152"
  },
  {
    "Building": "18G",
    "Room": "112HALL",
    "Location": "J-8-5",
    "BarCode": "P000031111"
  },
  {
    "Building": "18G",
    "Room": "112HALL",
    "Location": "J-8-5",
    "BarCode": "P166279435"
  },
  {
    "Building": "18G",
    "Room": "112HALL",
    "Location": "A-10-7",
    "BarCode": "P352831849"
  },
  {
    "Building": "18G",
    "Room": "5P04",
    "Location": "C-4-10",
    "BarCode": "P726526379"
  },
  {
    "Building": "18C",
    "Room": "6THST",
    "Location": "CAGE14",
    "BarCode": "P000453262"
  },
  {
    "Building": "18C",
    "Room": "6THST",
    "Location": "CAGE13",
    "BarCode": "P954732810"
  }];
  
let hierarchy = ['Building', 'Room', 'Location', 'BarCode'];

let output = {};
values.forEach(v => hierarchy.reduce((o, h) => o[v[h]] = o[v[h]] || {}, output));

console.log(output);

注意:以上内容中的代码部分未进行翻译,保持原文。

英文:

To get you started, you can use iterate through the array and hierarchy of keys to convert the array of objects into a nested hierarchy of objects. Here's a 1-liner:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let values = [
{
&quot;Building&quot;: &quot;18A&quot;,
&quot;Room&quot;: 219,
&quot;Location&quot;: &quot;E-4-6&quot;,
&quot;BarCode&quot;: &quot;P000019152&quot;
},
{
&quot;Building&quot;: &quot;18G&quot;,
&quot;Room&quot;: &quot;112HALL&quot;,
&quot;Location&quot;: &quot;J-8-5&quot;,
&quot;BarCode&quot;: &quot;P000031111&quot;
},
{
&quot;Building&quot;: &quot;18G&quot;,
&quot;Room&quot;: &quot;112HALL&quot;,
&quot;Location&quot;: &quot;J-8-5&quot;,
&quot;BarCode&quot;: &quot;P166279435&quot;
},
{
&quot;Building&quot;: &quot;18G&quot;,
&quot;Room&quot;: &quot;112HALL&quot;,
&quot;Location&quot;: &quot;A-10-7&quot;,
&quot;BarCode&quot;: &quot;P352831849&quot;
},
{
&quot;Building&quot;: &quot;18G&quot;,
&quot;Room&quot;: &quot;5P04&quot;,
&quot;Location&quot;: &quot;C-4-10&quot;,
&quot;BarCode&quot;: &quot;P726526379&quot;
},
{
&quot;Building&quot;: &quot;18C&quot;,
&quot;Room&quot;: &quot;6THST&quot;,
&quot;Location&quot;: &quot;CAGE14&quot;,
&quot;BarCode&quot;: &quot;P000453262&quot;
},
{
&quot;Building&quot;: &quot;18C&quot;,
&quot;Room&quot;: &quot;6THST&quot;,
&quot;Location&quot;: &quot;CAGE13&quot;,
&quot;BarCode&quot;: &quot;P954732810&quot;
}];
let hierarchy = [&#39;Building&#39;, &#39;Room&#39;, &#39;Location&#39;, &#39;BarCode&#39;];
let output = {};
values.forEach(v =&gt; hierarchy.reduce((o, h) =&gt; o[v[h]] = o[v[h]] || {}, output));
console.log(output);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月7日 02:16:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定