分组Json Arrary Obejct

时间:2018-02-01 06:36:22

标签: javascript arrays json grouping

我的源数据采用标准JSON格式。但我希望在分组中格式化数据,以便可以按照Catname和Catid对原始数据进行分组。

我已经尝试过前端的loadash库了。但是不想在前端使用它。

var originalData = [
{
    "quesid": 1,
    "uoptid": 0,
    "catid": 1,
    "catname": "Soft Skill"
},
{
    "quesid": 2,
    "uoptid": 0,
    "catid": 1,
    "catname": "Soft Skill"
},
{
    "quesid": 3,
    "uoptid": 0,
    "catid": 2,
    "catname": "Technical"
},
{
    "quesid": 4,
    "uoptid": 0,
    "catid": 2,
    "catname": "Technical"
}
]

如下所示:

        var transformData = [
        { 
          catname: "Soft Skill",
          catid : 1,
          ques : [ 
    {quesid : 1, uotp : 0 },
    {quesid : 2, uotp : 0 }
    ]
},
{
          catname: "Technical",
          catid : 2,
          ques : [ 
    {quesid : 3, uotp : 0 },
    {quesid : 4, uotp : 0 }
    ]
}
   ]

4 个答案:

答案 0 :(得分:0)

您可以使用array.prototype.reduce。如果ES6不是问题,则应该是:



var originalData = [{"quesid": 1, "uoptid": 0, "catid": 1, "catname": "Soft Skill"}, {"quesid": 2, "uoptid": 0, "catid": 1, "catname": "Soft Skill"}, {"quesid": 3, "uoptid": 0, "catid": 2, "catname": "Technical"},{"quesid": 4,"uoptid": 0,"catid": 2,"catname": "Technical"}];

var grouped = originalData.reduce((m, {quesid, uoptid, catid, catname}) => {
    var found = m.find(e => e.catid === catid);
    var quesItem = { quesid, uotp: uoptid };
    found ? found.ques.push(quesItem) : m.push({catname, catid, ques: [ quesItem ]});
    return m;
}, []);

console.log(grouped);




答案 1 :(得分:0)

实现此目的的一种方法是创建类别ID的映射并通过迭代源数组来构建数据。然后可以将结果映射转换回数组

样品:

var originalData = [{
        "quesid": 1,
        "uoptid": 0,
        "catid": 1,
        "catname": "Soft Skill"
    },
    {
        "quesid": 2,
        "uoptid": 0,
        "catid": 1,
        "catname": "Soft Skill"
    },
    {
        "quesid": 3,
        "uoptid": 0,
        "catid": 2,
        "catname": "Technical"
    },
    {
        "quesid": 4,
        "uoptid": 0,
        "catid": 2,
        "catname": "Technical"
    }
];

var map = originalData.reduce(function (m, d) {
    if (!m[d.catname]) {
        m[d.catname] = {
            catname: d.catname,
            catid: d.catid,
            ques: []
        }
    }
    m[d.catname].ques.push({
        quesid: d.quesid,
        uopt: d.uoptid
    });
    return m;
}, {});
var transformData = Object.keys(map).map(function (key) {
    return map[key];
});

答案 2 :(得分:0)

hash = OriginalData.reduce((p,c) => (p[c.catname] ? p[c.catname].push(c) : p[c.catname] = [c],p) ,{}),
newData = Object.keys(hash).map(k => ({catname: k, ques: hash[k]}));

console.log(newData)

答案 3 :(得分:0)

您可以使用带有哈希表和一些嵌套循环的直接方法,一个用于给定数据,另一个用于获取组合密钥。

var data = [{ quesid: 1, uoptid: 0, catid: 1, catname: "Soft Skill" }, { quesid: 2, uoptid: 0, catid: 1, catname: "Soft Skill" }, { quesid: 3, uoptid: 0, catid: 2, catname: "Technical" }, { quesid: 4, uoptid: 0, catid: 2, catname: "Technical" }],
    hash = Object.create(null),
    keys = ["catid", "catname"],
    i, j, key, o,
    grouped = [];

for (i = 0; i < data.length; i++) {
    key = "";
    o = data[i];
    for (j = 0; j < keys.length; j++) {
        key += (key && "|") + o[keys[j]];
    }
    if (!hash[key]) {
        hash[key] = { catname: o.catname, catid: o.catid, ques: [] };
        grouped.push(hash[key]);
    }
    hash[key].ques.push({ quesid: o.quesid, uotp: o.uoptid });
}

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