如何将以下对象数组解析为以下格式以进行显示 [英] How do I parse the following object array to the following format for displaying

查看:36
本文介绍了如何将以下对象数组解析为以下格式以进行显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是输入:

[{
    "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"
}]

期望输出

在一个数组中,每个部门应该有一个相关类别的数组,每个类别应该有一个相关的子类别数组,每个子类别应该有一个相关的产品数组

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

推荐答案

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

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

这篇关于如何将以下对象数组解析为以下格式以进行显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆