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

时间:2017-01-19 08:05:36

标签: javascript multidimensional-array

这是输入:

vector<void*>

期望输出

在单个数组中,每个Department应该有一个相关Category的数组,每个Category应该有一个相关Subcategory数组,每个Subcategory应该有一个相关产品数组

这样我就可以在单个数组中迭代显示附加的产品

enter image description here

3 个答案:

答案 0 :(得分:1)

执行此操作的最简单方法(假设所有数据都具有相同的对象签名):

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);

答案 1 :(得分:0)

您可以使用组更改检查最后的项目,并使用替换对象作为对象的给定键。

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; }

您可以将非规范化数据转换为树对象。

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; }

答案 2 :(得分:0)

您需要解析的是JSON数据。 我写了一个小脚本。您可以在此处找到它:https://jsfiddle.net/t4nn4bpj/43/

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;
            }
         }
      }

} }

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()"/>

这在textarea输出:

PSpace D1
C1类 子类别S1
产品P1

PSpace D1
C2类 子类别S2
产品P2

PSpace D1
C2类 子类别S3
产品P6

PSpace D2
类别C6
子类别S7
产品P7

PSpace D2
类别C6
子类别S7
产品P8

希望这会有所帮助。 乌尔里希