我的Json数据有问题,我的Json数据就像那样。
[
{
"category": "A",
"subcategory": "a",
"channel": "http://localhost/stream"
},
{
"category": "A",
"subcategory": "b",
"channel": "http://localhost/stream"
},
{
"category": "B",
"subcategory": "c",
"channel": "http://localhost/stream"
},
{
"category": "B",
"subcategory": "d",
"channel": "http://localhost/stream"
}
]
如何按类别名称排序以显示子类别。如何按类别获取数据
答案 0 :(得分:1)
如果我已正确理解,您希望按类别分组:
var d = {}
// Group elems by category
for (var i = 0; i < data.length; i++) {
var category = data[i]["category"];
if (!(category in d)) {
d[category] = [];
}
d[category].push(data[i]);
}
// Retrieve elements by category
d['A'] // [{"category": "A", ...}, {"category": "B", ...}]