简体   繁体   English

如何将以下对象数组解析为以下格式以显示

[英]How do I parse the following object array to the following format for displaying

This is the input: 这是输入:

[{
    "PSpace": "D1",
    "Category": "C1",
    "SubCategory": "S1",
    "Pname": "P1"
}, {
    "PSpace": "D1",
    "Category": "C2",
    "SubCategory": "S2",
    "Pname": "P2"
}, {
    "PSpace": "D1",
    "Category": "C2",
    "SubCategory": "S3",
    "Pname": "P6"
}, {
    "PSpace": "D2",
    "Category": "C6",
    "SubCategory": "S7",
    "Pname": "P7"
}, {
    "PSpace": "D2",
    "Category": "C6",
    "SubCategory": "S7",
    "Pname": "P8"
}]

Desired Output 期望的输出

In a single array each Department should have an array of related Category, each Category should have an array of related Subcategory and each Subcategory should have an array of related Products 在一个阵列中,每个部门应具有一个相关类别的阵列,每个类别应具有一个相关子类别的阵列,并且每个子类别应具有一个相关产品的阵列

so that i can iterate through a single array an display products as attached 这样我就可以遍历单个阵列来显示附加的显示产品

在此处输入图片说明

The simplest way to do this (assuming all your data has the same object signature): 最简单的方法(假设所有数据具有相同的对象签名):

var str = '';
for (var i = 0; i < input.length; i++) {
   for (var name in input[i]) {
       str += name + ' ' + input[i][name];
   }

   str += '\n';
}

console.log(str);

You could use a group change with a check of the last items and use a replacement object for the given keys of the object. 您可以使用组更改来检查最后一项,并为该对象的给定键使用替换对象。

 var data = [{ PSpace: "D1", Category: "C1", SubCategory: "S1", Pname: "P1" }, { PSpace: "D1", Category: "C2", SubCategory: "S2", Pname: "P2" }, { PSpace: "D1", Category: "C2", SubCategory: "S3", Pname: "P6" }, { PSpace: "D2", Category: "C6", SubCategory: "S7", Pname: "P7" }, { PSpace: "D2", Category: "C6", SubCategory: "S7", Pname: "P8" }], names = { PSpace: "Space", Category: "Category", SubCategory: "Sub Category", Pname: "Name" }, result = data.map(function (o) { var all = false, r = []; Object.keys(o).forEach(function (k, i) { if (i < 3) { if (all || this[k] !== o[k]) { r.push(names[k] + ' ' + o[k]); this[k] = o[k]; all = true; } } else { r.push(names[k] + ' ' + o[k]); } }, this); return r; }, {}); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You could transforn the not normalized data into a tree object. 您可以将未规范化的数据转换为树对象。

 var data = [{ PSpace: "D1", Category: "C1", SubCategory: "S1", Pname: "P1" }, { PSpace: "D1", Category: "C2", SubCategory: "S2", Pname: "P2" }, { PSpace: "D1", Category: "C2", SubCategory: "S3", Pname: "P6" }, { PSpace: "D2", Category: "C6", SubCategory: "S7", Pname: "P7" }, { PSpace: "D2", Category: "C6", SubCategory: "S7", Pname: "P8" }], names = { PSpace: "Space", Category: "Category", SubCategory: "Sub Category", Pname: "Name" }, result = data.reduce(function (t, o) { var path = Object.keys(o), last = path.pop(); path.reduce(function (r, k, i, kk) { return r[o[k]] = r[o[k]] || (i < kk.length - 1 ? {} : []); }, t).push(o[last]); return t; }, {}); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

What you need is to parse that JSON Data. 您需要解析该JSON数据。 I wrote a small script. 我写了一个小脚本。 You can find it here: https://jsfiddle.net/t4nn4bpj/43/ 您可以在这里找到它: https : //jsfiddle.net/t4nn4bpj/43/

The JS code looks like this: JS代码如下所示:

function stringContains(string, substring)
{
    return string.indexOf(substring) !== -1;
}
function parse()
{
        document.getElementById("myTextField").value = "";

    var jsonData = [{ "PSpace": "D1", "Category": "C1", "SubCategory": "S1", "Pname": "P1" }, { "PSpace": "D1", "Category": "C2", "SubCategory": "S2", "Pname": "P2" }, { "PSpace": "D1", "Category": "C2", "SubCategory": "S3", "Pname": "P6" }, { "PSpace": "D2", "Category": "C6", "SubCategory": "S7", "Pname": "P7" }, { "PSpace": "D2", "Category": "C6", "SubCategory": "S7", "Pname": "P8" }];

  for(var obj in jsonData)
  {
     if(jsonData.hasOwnProperty(obj))
     {
        for(var prop in jsonData[obj])
        {
            if(jsonData[obj].hasOwnProperty(prop))
            {
              var output = prop+" "+jsonData[obj][prop]+"\n";
                if(stringContains(prop,"Pname"))
                {
                    output = "Product "+jsonData[obj][prop]+"\n\n";
                }
              document.getElementById("myTextField").value += output;
            }
         }
      }

} } }}

The hmtl: hmtl:

<h1 id="title">Parse JSON</h1>
<textarea id="myTextField" rows="30">click the button</textarea>
<input type="submit" id="byBtn" value="parse" onclick="parse()"/>

This outputs this in the textarea: 这将在文本区域中输出:

PSpace D1 太空D1
Category C1 C1类
SubCategory S1 子类别S1
Product P1 产品P1

PSpace D1 太空D1
Category C2 C2类
SubCategory S2 子类别S2
Product P2 产品P2

PSpace D1 太空D1
Category C2 C2类
SubCategory S3 子类别S3
Product P6 产品P6

PSpace D2 PSpace D2
Category C6 C6类
SubCategory S7 子类别S7
Product P7 产品P7

PSpace D2 PSpace D2
Category C6 C6类
SubCategory S7 子类别S7
Product P8 产品P8

Hope this helps. 希望这可以帮助。 Ulrich 乌尔里希

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM