我在json中有一个对象数组,如下所示:
[{
"vehicleid": 3,
"name": "Teste2VDD",
"brand": "Scania",
"model": "6x2",
"class": "class1",
"region": "Curitiba",
"customer": "Cliente A",
"customerid": 1
},
{
"vehicleid": 1,
"name": "Teste1",
"brand": "Volkswagen",
"model": "5x3",
"class": "class1",
"region": "Curitiba",
"customer": "Cliente A",
"customerid": 1
},
{
"vehicleid": 2,
"name": "Teste3VDD",
"brand": "Volkswagen",
"model": "6x2",
"class": "class2",
"region": "Curitiba",
"customer": "Cliente A",
"customerid": 1
},
{
"vehicleid": 5,
"name": "Teste4VDD",
"brand": "Scania",
"model": "6x4",
"class": "class2",
"region": "Curitiba",
"customer": "Novo",
"customerid": 2
},
{
"vehicleid": 6,
"name": "Teste5VDD",
"brand": "Scania",
"model": "5x5",
"class": "class2",
"region": "Curitiba",
"customer": "Novo",
"customerid": 2
},
{
"vehicleid": 7,
"name": "veiculo",
"brand": "Scania",
"model": "5x4",
"class": "class1",
"region": "Porto Alegre",
"customer": "Cliente3",
"customerid": 3
},
{
"vehicleid": 8,
"name": "veiculo65",
"brand": "Volkswagen",
"model": "5x4",
"class": "class3",
"region": "Porto Alegre",
"customer": "Cliente3",
"customerid": 3
},
{
"vehicleid": 11,
"name": "Veiculo de teste h",
"brand": "Scania",
"model": "5x3",
"class": "class3",
"region": "Belo Horizonte",
"customer": "Cliente de teste h",
"customerid": 10
},
{
"vehicleid": 13,
"name": "Nome 3",
"brand": "Volkswagen",
"model": "6x3",
"class": "class3",
"region": "Belo Horizonte",
"customer": "Cliente de teste h",
"customerid": 10
},
{
"vehicleid": 12,
"name": "Nome",
"brand": "Volvo",
"model": "6x3",
"class": "class3",
"region": "Belo Horizonte",
"customer": "Cliente de teste h",
"customerid": 10
}]
我需要创建一个树hierarquia,具体取决于我作为参数传递的过滤器。
第一级始终是客户,最后一级始终是车辆。
例如:
1 - 如果我选择通过客户过滤树,则客户将订购该树,然后客户的子项将是车辆,像这样:
[{
"attr": {
"type": "customer"
},
"text": "Cliente A",
"children": [
{
"id": 1,
"text": "Teste1",
"attr": {
"type": "vehicle",
"title": "Teste1"
}
},
{
"id": 2,
"text": "Teste3VDD",
"attr": {
"type": "vehicle",
"title": "Teste3VDD"
}
},
{
"id": 3,
"text": "Teste2VDD",
"attr": {
"type": "vehicle",
"title": "Teste2VDD"
}
}
]
},
{
"attr": {
"type": "customer"
},
"text": "Novo",
"children": [
{
"id": 5,
"text": "Teste4VDD",
"attr": {
"type": "vehicle",
"title": "Teste4VDD"
}
},
{
"id": 6,
"text": "Teste5VDD",
"attr": {
"type": "vehicle",
"title": "Teste5VDD"
}
}
]
},
{
"attr": {
"type": "customer"
},
"text": "Cliente3",
"children": [
{
"id": 7,
"text": "veiculo",
"attr": {
"type": "vehicle",
"title": "veiculo"
}
},
{
"id": 8,
"text": "veiculo65",
"attr": {
"type": "vehicle",
"title": "veiculo65"
}
}
]
},
{
"attr": {
"type": "customer"
},
"text": "Cliente",
"children": [
{
"id": 11,
"text": "Veiculo de teste h",
"attr": {
"type": "vehicle",
"title": "Veiculo de teste h"
}
},
{
"id": 12,
"text": "Nome",
"attr": {
"type": "vehicle",
"title": "Nome"
}
},
{
"id": 13,
"text": "Nome 3",
"attr": {
"type": "vehicle",
"title": "Nome 3"
}
}
]
}]
2 - 如果我选择按品牌过滤树,则树将由客户订购,然后是客户的子项将是品牌,然后品牌的孩子将车辆
3 - 如果我选择按品牌和模型过滤树,则树将由客户订购,然后是客户将是品牌,然后品牌的孩子将是模型,然后是< strong>模型将车辆
4 - 如果我选择按品牌,型号和&#34;区域&#34;过滤树,则客户将订购树,然后客户的孩子将是品牌,那么品牌的孩子将品牌< / strong>那么模型的孩子将是区域,那么区域的孩子将是车辆
等等......
任何人都知道如何做到这一点?
提前致谢!
答案 0 :(得分:1)
您可以使用动态方法,通过给定键或具有组名和键的对象进行分组,并使用每个级别的哈希表构建嵌套结构。
function groupBy(array, keys) {
var result = [],
temp = { _: result };
data.forEach(function (a) {
keys.reduce(function (r, k, i, kk) {
var type = typeof k === 'object' ? Object.keys(k)[0] : k,
key = typeof k === 'object' ? a[k[type]] : a[k];
if (!r[key]) {
r[key] = { _: [] };
r._.push(i + 1 < kk.length
? { attr: { type: type }, text: key, children: r[key]._ }
: { id: a.vehicleid, text: key, attr: { type: type, title: key } }
);
}
return r[key];
}, temp);
});
return result;
}
var data = [{ vehicleid: 3, name: "Teste2VDD", brand: "Scania", model: "6x2", class: "class1", region: "Curitiba", customer: "Cliente A", customerid: 1 }, { vehicleid: 1, name: "Teste1", brand: "Volkswagen", model: "5x3", class: "class1", region: "Curitiba", customer: "Cliente A", customerid: 1 }, { vehicleid: 2, name: "Teste3VDD", brand: "Volkswagen", model: "6x2", class: "class2", region: "Curitiba", customer: "Cliente A", customerid: 1 }, { vehicleid: 5, name: "Teste4VDD", brand: "Scania", model: "6x4", class: "class2", region: "Curitiba", customer: "Novo", customerid: 2 }, { vehicleid: 6, name: "Teste5VDD", brand: "Scania", model: "5x5", class: "class2", region: "Curitiba", customer: "Novo", customerid: 2 }, { vehicleid: 7, name: "veiculo", brand: "Scania", model: "5x4", class: "class1", region: "Porto Alegre", customer: "Cliente3", customerid: 3 }, { vehicleid: 8, name: "veiculo65", brand: "Volkswagen", model: "5x4", class: "class3", region: "Porto Alegre", customer: "Cliente3", customerid: 3 }, { vehicleid: 11, name: "Veiculo de teste h", brand: "Scania", model: "5x3", class: "class3", region: "Belo Horizonte", customer: "Cliente de teste h", customerid: 10 }, { vehicleid: 13, name: "Nome 3", brand: "Volkswagen", model: "6x3", class: "class3", region: "Belo Horizonte", customer: "Cliente de teste h", customerid: 10 }, { vehicleid: 12, name: "Nome", brand: "Volvo", model: "6x3", class: "class3", region: "Belo Horizonte", customer: "Cliente de teste h", customerid: 10 }];
console.log(groupBy(data, ['customer', { vehicle: 'name' }]));
console.log(groupBy(data, ['brand', 'customer', { vehicle: 'name' }]));
.as-console-wrapper { max-height: 100% !important; top: 0; }