- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个像这样的 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
}]
我需要根据我作为参数传递的过滤器创建一个树层次结构。
第一个级别始终是客户,最后一个级别始终是车辆。
例如:
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 - 如果我选择按品牌、型号和“地区”过滤树,树将按客户排序,那么客户的子代将是品牌,那么品牌的子代将是模型,然后Model 的子级将是 Region,那么 Region 的子级将是 Vehicles
等等...
有人知道可以做这个吗?
提前致谢!
最佳答案
您可以使用动态方法,按给定键或具有组名称和键的对象进行分组,并通过为每个级别使用哈希表来构建嵌套结构。
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; }
关于javascript - 从多级对象数组创建 JSON 树结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46136958/
我正在扩展 baseClass 监听器并尝试读取一些值,但是顺序中似乎没有任何层次结构。 我的语法的精简版如下: start: config_options+ config_options: (KEY
XML文档是一种树形结构,它从 根部 开始,然后扩展到 枝叶 一个 XML 文档范例 XML文档使用的是简单的具有自我描述性的语法 我们拿之间的小红写给小明的情书作为范例 <?xml v
要了解我想做什么,这里有一个简单的解释:我想创建模板。 一个"template"有一个“面板”这个“面板”有一些“项目”和另一个“面板”我们必须一步一步地做,直到最后一个“Item” 我想要这样的东西
我的数据结构如下。 A A1 A2 B B1 C C1 C2 C3 这些信息转移到两个名为group1和group2的表中。 group1有第一层数据和中间层数据。 gro
所以我有一个如下所示的树结构,其中我在叶节点中有一个计数属性。我想总结计数并将计数总和与其父项进行比较。比 parent1 和 parent2 有他们的 child 的总和。然后从那里继续,所以 gr
我有以下 json : var jsonObj = [ { "parentIndex": '0' ,
我正在尝试创建一个包含任意数量子类别的类别列表,其中子类别也可以有自己的子类别。 我从 Mysql 数据库中选择了所有类别,猫在标准关联数组列表中,每个类别都有一个 id、名称、parentid,如果
我想动态构建层次结构,每个节点创建为层次结构中的一个层/级别,具有自己的节点数组。这应该形成一个树结构。应该有一个根节点,以及未定义数量的节点和级别来构成层次结构大小。除了根节点之外,什么都不应该被修
我正在浏览 Wikitravel API,我注意到他们提供的 XML 文件只是将所有信息集中在一个大 blob 中。示例:http://wikitravel.org/en/Special:Export
我正在尝试设置一个UITableView来显示嵌套的线程项目。我正在使用 Core Data 和 NSFetchedResultsController,但不确定我的方法应该是什么。我想显示这样的信息:
我有一个这样的表数据库: tree{id,name,parent}content{id,content,parent}tree 表包含一个树状结构,如果 parent 是 0,它是顶级元素,如果不同,
我有一个像这样的 json 对象数组: [{ "vehicleid": 3, "name": "Teste2VDD", "brand": "Scania", "mode
我有一个 json 树结构,通过按此 fiddle 上的调用来附加该结构:http://jsfiddle.net/adrianjsfiddlenetuser/C6Ssa/4/ 按 fiddle 上的调
我需要使用 prefuse 可视化一个 xml 文件,但由于其大小,我必须一次仅加载文件的一部分,并将它们表示为 prefuse 中的树结构。我设法加载并可视化第一棵树,其中包含所有 xml 元素的深
我有一个包含如下数据的文件: ID attribute 1 'text' 101 'text' 1011 'text' 10111 'text' 1011101 'text' 1011102 'tex
这是我的数据结构: { projects: [ { revisions: [ { files: [] }, ], } ], us
我有一个列表需要转换成json格式。 这个.. var sourceList = [ { title: "item-1", indent: "0" }, { title: "item-
我希望我的内容有一个树/文件夹结构,但希望所有页面都作为一个平面 URL。例如 位于/cat1/subcat2/tulips.html 的页面将在以下位置提供: http://example.com/
简单来说 我有一个由对象组成的树结构。 是否可以构建该树并向每个对象添加对其父对象的引用? 我知道引用适用于对象,但我不确定在这种情况下是否有效? 我希望能够写出这样的东西 currentLevel
我创建了一个涉及学校学生的项目。当我开始构建这个项目时,我遇到了三个主要问题: 1)推荐或更好的 JSON 树结构? schools grades users(uids) in
我是一名优秀的程序员,十分优秀!