- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
经过一番麻烦的斗争,我几乎想出了如何将平面 json 文件转换为分层文件。这个函数不是我自己写的。我从下面的帖子中复制了它。
但现在的问题是,帖子中编写的函数只有两级层次结构。但我正在寻找 4 级层次结构。我尝试重写失败的函数,但是。
用我正在尝试的代码进行编码。
var data = [ { "dep": "First Top", "name": "First child", "model": "value1", "size": "320" }, { "dep": "First Top", "name": "First child", "model": "value2", "size": "320" }, { "dep": "First Top", "name": "First child", "model": "value3", "size": "320" }, { "dep": "First Top", "name": "First child", "model": "value4", "size": "320" }, { "dep": "First Top", "name": "SECOND CHILD", "model": "value1", "size": "320" }, { "dep": "First Top", "name": "SECOND CHILD", "model": "value2", "size": "320" }, { "dep": "First Top", "name": "SECOND CHILD", "model": "value3", "size": "320" }, { "dep": "First Top", "name": "SECOND CHILD", "model": "value4", "size": "320" }, { "dep": "Second Top", "name": "First Child", "model": "value1", "size": "320" }, { "dep": "Second Top", "name": "First Child", "model": "value2", "size": "320" }, { "dep": "Second Top", "name": "First Child", "model": "value3", "size": "320" }, { "dep": "Second Top", "name": "First Child", "model": "value4", "size": "320" }, { "dep": "Second Top", "name": "SECOND CHILD", "model": "value1", "size": "320" }, { "dep": "Second Top", "name": "SECOND CHILD", "model": "value2", "size": "320" }, { "dep": "Second Top", "name": "SECOND CHILD", "model": "value3", "size": "320" }, { "dep": "Second Top", "name": "SECOND CHILD", "model": "value4", "size": "320" }, { "dep": "Third Top", "name": "First Child", "model": "value2", "size": "320" }, { "dep": "Third Top", "name": "First Child", "model": "value3", "size": "320" }, { "dep": "Third Top", "name": "First Child", "model": "value4", "size": "320" }, { "dep": "Third Top", "name": "First Child", "model": "value5", "size": "320" }, { "dep": "Third Top", "name": "Second Child", "model": "value1", "size": "320" }, { "dep": "Third Top", "name": "Second Child", "model": "value2", "size": "320" }, { "dep": "Third Top", "name": "Second Child", "model": "value3", "size": "320" }, { "dep": "Third Top", "name": "Second Child", "model": "value4", "size": "320" } ] var newData = {"name":"root", "children":{}} data.forEach(function(d){ if(typeof newData.children[d.dep] !== 'undefined') { newData.children[d.dep].children.push(d) } else { newData.children[d.dep] = {"name": d.dep, "children": [{"name": d.name, "children": [{"name": d.model, "size": d.size}]}]} } }) newData.children = Object.keys(newData.children).map(function (key) { return newData.children[key]; }); // show what we've got d3.select('body').append('pre') .text(JSON.stringify(newData, null, ' '));
当前代码的输出
{ "name": "root", "children": [ { "name": "First Top", "children": [ { "name": "First child", "children": [ { "name": "value1", "size": "320" } ] }, { "dep": "First Top", "name": "First child", "model": "value2", "size": "320" }, { "dep": "First Top", "name": "First child", "model": "value3", "size": "320" }, { "dep": "First Top", "name": "First child", "model": "value4", "size": "320" }, { "dep": "First Top", "name": "SECOND CHILD", "model": "value1", "size": "320" }, { "dep": "First Top", "name": "SECOND CHILD", "model": "value2", "size": "320" }, { "dep": "First Top", "name": "SECOND CHILD", "model": "value3", "size": "320" }, { "dep": "First Top", "name": "SECOND CHILD", "model": "value4", "size": "320" } ] }, { "name": "Second Top", "children": [ { "name": "First Child", "children": [ { "name": "value1", "size": "320" } ] }, { "dep": "Second Top", "name": "First Child", "model": "value2", "size": "320" }, { "dep": "Second Top", "name": "First Child", "model": "value3", "size": "320" }, { "dep": "Second Top", "name": "First Child", "model": "value4", "size": "320" }, { "dep": "Second Top", "name": "SECOND CHILD", "model": "value1", "size": "320" }, { "dep": "Second Top", "name": "SECOND CHILD", "model": "value2", "size": "320" }, { "dep": "Second Top", "name": "SECOND CHILD", "model": "value3", "size": "320" }, { "dep": "Second Top", "name": "SECOND CHILD", "model": "value4", "size": "320" } ] }, { "name": "Third Top", "children": [ { "name": "First Child", "children": [ { "name": "value2", "size": "320" } ] }, { "dep": "Third Top", "name": "First Child", "model": "value3", "size": "320" }, { "dep": "Third Top", "name": "First Child", "model": "value4", "size": "320" }, { "dep": "Third Top", "name": "First Child", "model": "value5", "size": "320" }, { "dep": "Third Top", "name": "Second Child", "model": "value1", "size": "320" }, { "dep": "Third Top", "name": "Second Child", "model": "value2", "size": "320" }, { "dep": "Third Top", "name": "Second Child", "model": "value3", "size": "320" }, { "dep": "Third Top", "name": "Second Child", "model": "value4", "size": "320" } ] } ] }
所需的输出格式:
{ "name": "root", "children": [ { "name": "First Top", "children": [ { "name": "First child", "children": [ { "name": "value1", "size": "320" }, { "name": "value2", "size": "320" }, { "name": "value3", "size": "320" }, { "name": "value4", "size": "320" } ] }, { "name": "Second child", "children": [ { "name": "value1", "size": "320" }, { "name": "value2", "size": "320" }, { "name": "value3", "size": "320" }, { "name": "value4", "size": "320" } ] }, ] }, { "name": "Second Top", "children": [ { "name": "First child", "children": [ { "name": "value1", "size": "320" }, { "name": "value2", "size": "320" }, { "name": "value3", "size": "320" }, { "name": "value4", "size": "320" } ] }, { "name": "Second child", "children": [ { "name": "value1", "size": "320" }, { "name": "value2", "size": "320" }, { "name": "value3", "size": "320" }, { "name": "value4", "size": "320" } ] }, ] }, { "name": "Third Top", "children": [ { "name": "First child", "children": [ { "name": "value1", "size": "320" }, { "name": "value2", "size": "320" }, { "name": "value3", "size": "320" }, { "name": "value4", "size": "320" } ] }, { "name": "Second child", "children": [ { "name": "value1", "size": "320" }, { "name": "value2", "size": "320" }, { "name": "value3", "size": "320" }, { "name": "value4", "size": "320" } ] }, ] } ] }
我已经一周了,但我一个人无法弄清楚。有人请修改函数以获取我更新的分层格式的数据。
提前致谢!!
最佳答案
更新为使用递归方法
这应该适用于 n
个级别,而不仅仅是 2 或 3 个级别。您只需指定哪些属性定义哪些级别。
var data = [
{ "dep": "First Top", "name": "First child", "model": "value1", "size": "320" },
{ "dep": "First Top", "name": "First child", "model": "value2", "size": "320" },
{ "dep": "First Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
{ "dep": "Second Top", "name": "First Child", "model": "value1", "size": "320" }
];
var newData = { name :"root", children : [] },
levels = ["dep","name"];
// For each data row, loop through the expected levels traversing the output tree
data.forEach(function(d){
// Keep this as a reference to the current level
var depthCursor = newData.children;
// Go down one level at a time
levels.forEach(function( property, depth ){
// Look to see if a branch has already been created
var index;
depthCursor.forEach(function(child,i){
if ( d[property] == child.name ) index = i;
});
// Add a branch if it isn't there
if ( isNaN(index) ) {
depthCursor.push({ name : d[property], children : []});
index = depthCursor.length - 1;
}
// Now reference the new child array as we go deeper into the tree
depthCursor = depthCursor[index].children;
// This is a leaf, so add the last element to the specified branch
if ( depth === levels.length - 1 ) depthCursor.push({ name : d.model, size : d.size });
});
});
关于javascript - 将平面 JSON 文件转换为分层 json 数据,如flare.json [d3 示例文件],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19317115/
如何在 Flare by 2Dimensions 上通过“编辑顶点”选项使用关键帧? 我的用例:我想通过拖动顶点将矩形转换为不同的形状来为矩形设置动画。 最佳答案 单击右侧面板中的“转换为路径”。然后
我正在尝试让 Flare 动画在 Flutter 中运行。为此,我尝试了这个: https://github.com/tarek360/Boring-Star-Animation 实际上,该代码工作
我试图在从网络加载图像时显示图像占位符,并将占位符替换为带有淡入动画的实际图像。当我使用 Assets 中的简单 png 图像时,这可以正常工作。 以下是现有代码: FadeInImage(place
我的原始 Flare 文件/动画为其所有动画定义了一条三次曲线。但是当我在我的 Flutter 应用程序上导出并运行相同的动画时,它似乎使用了线性曲线。我已经多次导出文件并进行了不同的更改,但没有成功
我试图在 Flutter Web(开发 channel - v1.13.2)上创建一个简单的网页,但出现了这个奇怪的问题。当我尝试将 Flare 动画放置在分别具有 2 个附加小部件、背景和居中文本的
我已经开发了我的作品集,并且在本地一切正常,但当我将它上传到我的主机时,CSS 呈现不同。 我在 Chrome 上用力刷新,也在 Firefox 上尝试过,但仍然不同。 问题和解决方案是什么? 这是我
我在 python 中有这样的数据: [['a', 'b', 'c', 50], ['a', 'b', 'd', 100], ['a', 'b', 'e', 67], ['a', 'g', 'c
我正在尝试使用 MadCap Flare 元素生成 pdf 文件,但生成的 PDF 文件字体错误。我使用的是最新版本的 Flare,2019r2。 我正在尝试使用 Duotype 的 Flexo 字体
我有一个 networkx 图 g 我想画出这个可视化 http://mbostock.github.io/d3/talk/20111018/tree.html 这意味着我必须以某种方式将我的图形转换
我正在尝试在 Flutter 中重新播放 Flare 动画。动画完成后不循环播放。我想要一个按需播放的动画,同样的动画。 当我在动画之间切换时,只要交换字符串并调用 setState 就可以正常工作。
更新:我在其底部添加了一个codesanbox,其中包含我需要完成的操作的更详细说明。 所以我不太明白类在 React 中是如何工作的,我是 React 新手,并且使用过一点 useState,但从来
我目前正在与 Treemap 合作D3.js 的可视化,希望了解示例中使用的flare.json 是如何组织的。 json 输入文件的格式是否需要与示例中使用的结构完全相同。我有一个来自网络爬虫的输入
我正在使用众所周知的 flare.json 文件: { "name": "flare", "children": [ { "name": "analytics", "childre
我和我的同事正在使用 D3 可视化。具体来说,我们从 this example 开始.此可视化似乎使用了与 flare API 相关的某种 json。 在示例 json 中,有如下几行: "name
我有一个 defaultdict(list) 并且我使用了 simplejson.dumps(my_defaultdict) 以将 defaultdict 输出为 JSON 格式.我正在使用 http
我有一个扁平的 json 文件结构,例如: [ { "name" : "ABC", "parent":"DEF", "relation": "ghi", "depth": 1 }, { "name
在我的 D3 Flare Graph 中,当每个圆圈只有一个标签时,标题和标签会重叠。我想渲染内部标签,避免与圆圈标题重叠,它在每个圆圈的中间渲染。 var flareGraph = function
我有一个像这样的 json: { "a": { "x": { "y": { "a": {}, "z": {},
我在我的网站上遇到了这个奇怪的 SSL 问题。该域指向 Cloudflare,我正在使用灵活的 SSL 设置。通用默认证书在 Cloudflare 上设置。现在我所有的设备(android、ios、w
我使用的是 Cloud Flare Flexible SSL。我想为除 rss 提要之外的所有站点强制使用 SSL。 我试着用下面的代码来做,但得到了 ERR_TOO_MANY_REDIRECTS R
我是一名优秀的程序员,十分优秀!