- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个嵌套 JS 对象,如下所示:
data = {
"name": "root",
"children": [{
"name": "bob",
"children": []
},
{
"name": "susan",
"children": []
},
{
"name": "chris",
"children": []
},
{
"name": "bobo",
"children": []
},
{
"name": "heather",
"children": []
}
]
}
通过使用以下递归函数,我可以删除任何属性,无论深度如何:
function search_and_delete(obj, search_term) {
for (var k in obj) {
if (typeof obj[k] == "object" && obj[k] !== null) {
search_and_delete(obj[k], search_term)
} else {
if (typeof(obj.name) !== 'undefined') {
if (obj.name === search_term) {
delete obj.name
delete obj.children
}
}
}
}
}
例如:
search_and_delete(data, 'bob')
...产生以下结果:
{
"name": "root",
"children": [
{}, // <------ how do I remove this
{
"name": "susan",
"children": []
},
{
"name": "chris",
"children": []
},
{
"name": "bobo",
"children": []
},
{
"name": "heather",
"children": []
}
]
}
但是这在鲍勃曾经所在的位置留下了一个空物体。我怎样才能删除这个对象并重新索引 JSON 结构,就好像它从未存在过一样?
更新
@CertainPerformance 答案是最接近的,但会产生以下错误:
如果你使用这个照明弹data :
并运行以下命令:
search_and_delete(data, "Interpolator", data)
它的作用是:
最佳答案
一种选择是将父数组传递给递归调用,以便您可以从中拼接
找到的项。
因为改变数组会改变每个属性指向的内容,这将使使用 for..in
变得困难 - 使用 Object.values
来迭代对象(或数组):
const data = {
"name": "root",
"children": [{
"name": "bob",
"children": []
},
{
"name": "susan",
"children": []
},
{
"name": "chris",
"children": []
},
{
"name": "bobo",
"children": []
},
{
"name": "heather",
"children": []
}
]
}
function search_and_delete(obj, search_term, parent) {
if (obj.name === search_term) {
parent.splice(parent.indexOf(obj), 1);
} else {
Object.values(obj).forEach((val) => {
if (val && typeof val === "object") {
search_and_delete(val, search_term, obj)
}
});
}
}
search_and_delete(data, 'bob');
console.log(data);
使用更深层次结构的示例:
const data = {
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster", "value": 3938},
{"name": "CommunityStructure", "value": 3812},
{"name": "HierarchicalCluster", "value": 6714},
{"name": "MergeEdge", "value": 743}
]
},
{
"name": "graph",
"children": [
{"name": "BetweennessCentrality", "value": 3534},
{"name": "LinkDistance", "value": 5731},
{"name": "MaxFlowMinCut", "value": 7840},
{"name": "ShortestPaths", "value": 5914},
{"name": "SpanningTree", "value": 3416}
]
},
{
"name": "optimization",
"children": [
{"name": "AspectRatioBanker", "value": 7074}
]
}
]
},
{
"name": "animate",
"children": [
{"name": "Easing", "value": 17010},
{"name": "FunctionSequence", "value": 5842},
{
"name": "interpolate",
"children": [
{"name": "ArrayInterpolator", "value": 1983},
{"name": "ColorInterpolator", "value": 2047},
{"name": "DateInterpolator", "value": 1375},
{"name": "Interpolator", "value": 8746},
{"name": "MatrixInterpolator", "value": 2202},
{"name": "NumberInterpolator", "value": 1382},
{"name": "ObjectInterpolator", "value": 1629},
{"name": "PointInterpolator", "value": 1675},
{"name": "RectangleInterpolator", "value": 2042}
]
},
{"name": "ISchedulable", "value": 1041},
{"name": "Parallel", "value": 5176},
{"name": "Pause", "value": 449},
{"name": "Scheduler", "value": 5593},
{"name": "Sequence", "value": 5534},
{"name": "Transition", "value": 9201},
{"name": "Transitioner", "value": 19975},
{"name": "TransitionEvent", "value": 1116},
{"name": "Tween", "value": 6006}
]
},
{
"name": "data",
"children": [
{
"name": "converters",
"children": [
{"name": "Converters", "value": 721},
{"name": "DelimitedTextConverter", "value": 4294},
{"name": "GraphMLConverter", "value": 9800},
{"name": "IDataConverter", "value": 1314},
{"name": "JSONConverter", "value": 2220}
]
},
{"name": "DataField", "value": 1759},
{"name": "DataSchema", "value": 2165},
{"name": "DataSet", "value": 586},
{"name": "DataSource", "value": 3331},
{"name": "DataTable", "value": 772},
{"name": "DataUtil", "value": 3322}
]
},
{
"name": "display",
"children": [
{"name": "DirtySprite", "value": 8833},
{"name": "LineSprite", "value": 1732},
{"name": "RectSprite", "value": 3623},
{"name": "TextSprite", "value": 10066}
]
},
{
"name": "flex",
"children": [
{"name": "FlareVis", "value": 4116}
]
},
{
"name": "physics",
"children": [
{"name": "DragForce", "value": 1082},
{"name": "GravityForce", "value": 1336},
{"name": "IForce", "value": 319},
{"name": "NBodyForce", "value": 10498},
{"name": "Particle", "value": 2822},
{"name": "Simulation", "value": 9983},
{"name": "Spring", "value": 2213},
{"name": "SpringForce", "value": 1681}
]
},
{
"name": "query",
"children": [
{"name": "AggregateExpression", "value": 1616},
{"name": "And", "value": 1027},
{"name": "Arithmetic", "value": 3891},
{"name": "Average", "value": 891},
{"name": "BinaryExpression", "value": 2893},
{"name": "Comparison", "value": 5103},
{"name": "CompositeExpression", "value": 3677},
{"name": "Count", "value": 781},
{"name": "DateUtil", "value": 4141},
{"name": "Distinct", "value": 933},
{"name": "Expression", "value": 5130},
{"name": "ExpressionIterator", "value": 3617},
{"name": "Fn", "value": 3240},
{"name": "If", "value": 2732},
{"name": "IsA", "value": 2039},
{"name": "Literal", "value": 1214},
{"name": "Match", "value": 3748},
{"name": "Maximum", "value": 843},
{
"name": "methods",
"children": [
{"name": "add", "value": 593},
{"name": "and", "value": 330},
{"name": "average", "value": 287},
{"name": "count", "value": 277},
{"name": "distinct", "value": 292},
{"name": "div", "value": 595},
{"name": "eq", "value": 594},
{"name": "fn", "value": 460},
{"name": "gt", "value": 603},
{"name": "gte", "value": 625},
{"name": "iff", "value": 748},
{"name": "isa", "value": 461},
{"name": "lt", "value": 597},
{"name": "lte", "value": 619},
{"name": "max", "value": 283},
{"name": "min", "value": 283},
{"name": "mod", "value": 591},
{"name": "mul", "value": 603},
{"name": "neq", "value": 599},
{"name": "not", "value": 386},
{"name": "or", "value": 323},
{"name": "orderby", "value": 307},
{"name": "range", "value": 772},
{"name": "select", "value": 296},
{"name": "stddev", "value": 363},
{"name": "sub", "value": 600},
{"name": "sum", "value": 280},
{"name": "update", "value": 307},
{"name": "variance", "value": 335},
{"name": "where", "value": 299},
{"name": "xor", "value": 354},
{"name": "_", "value": 264}
]
},
{"name": "Minimum", "value": 843},
{"name": "Not", "value": 1554},
{"name": "Or", "value": 970},
{"name": "Query", "value": 13896},
{"name": "Range", "value": 1594},
{"name": "StringUtil", "value": 4130},
{"name": "Sum", "value": 791},
{"name": "Variable", "value": 1124},
{"name": "Variance", "value": 1876},
{"name": "Xor", "value": 1101}
]
},
{
"name": "scale",
"children": [
{"name": "IScaleMap", "value": 2105},
{"name": "LinearScale", "value": 1316},
{"name": "LogScale", "value": 3151},
{"name": "OrdinalScale", "value": 3770},
{"name": "QuantileScale", "value": 2435},
{"name": "QuantitativeScale", "value": 4839},
{"name": "RootScale", "value": 1756},
{"name": "Scale", "value": 4268},
{"name": "ScaleType", "value": 1821},
{"name": "TimeScale", "value": 5833}
]
},
{
"name": "util",
"children": [
{"name": "Arrays", "value": 8258},
{"name": "Colors", "value": 10001},
{"name": "Dates", "value": 8217},
{"name": "Displays", "value": 12555},
{"name": "Filter", "value": 2324},
{"name": "Geometry", "value": 10993},
{
"name": "heap",
"children": [
{"name": "FibonacciHeap", "value": 9354},
{"name": "HeapNode", "value": 1233}
]
},
{"name": "IEvaluable", "value": 335},
{"name": "IPredicate", "value": 383},
{"name": "IValueProxy", "value": 874},
{
"name": "math",
"children": [
{"name": "DenseMatrix", "value": 3165},
{"name": "IMatrix", "value": 2815},
{"name": "SparseMatrix", "value": 3366}
]
},
{"name": "Maths", "value": 17705},
{"name": "Orientation", "value": 1486},
{
"name": "palette",
"children": [
{"name": "ColorPalette", "value": 6367},
{"name": "Palette", "value": 1229},
{"name": "ShapePalette", "value": 2059},
{"name": "SizePalette", "value": 2291}
]
},
{"name": "Property", "value": 5559},
{"name": "Shapes", "value": 19118},
{"name": "Sort", "value": 6887},
{"name": "Stats", "value": 6557},
{"name": "Strings", "value": 22026}
]
},
{
"name": "vis",
"children": [
{
"name": "axis",
"children": [
{"name": "Axes", "value": 1302},
{"name": "Axis", "value": 24593},
{"name": "AxisGridLine", "value": 652},
{"name": "AxisLabel", "value": 636},
{"name": "CartesianAxes", "value": 6703}
]
},
{
"name": "controls",
"children": [
{"name": "AnchorControl", "value": 2138},
{"name": "ClickControl", "value": 3824},
{"name": "Control", "value": 1353},
{"name": "ControlList", "value": 4665},
{"name": "DragControl", "value": 2649},
{"name": "ExpandControl", "value": 2832},
{"name": "HoverControl", "value": 4896},
{"name": "IControl", "value": 763},
{"name": "PanZoomControl", "value": 5222},
{"name": "SelectionControl", "value": 7862},
{"name": "TooltipControl", "value": 8435}
]
},
{
"name": "data",
"children": [
{"name": "Data", "value": 20544},
{"name": "DataList", "value": 19788},
{"name": "DataSprite", "value": 10349},
{"name": "EdgeSprite", "value": 3301},
{"name": "NodeSprite", "value": 19382},
{
"name": "render",
"children": [
{"name": "ArrowType", "value": 698},
{"name": "EdgeRenderer", "value": 5569},
{"name": "IRenderer", "value": 353},
{"name": "ShapeRenderer", "value": 2247}
]
},
{"name": "ScaleBinding", "value": 11275},
{"name": "Tree", "value": 7147},
{"name": "TreeBuilder", "value": 9930}
]
},
{
"name": "events",
"children": [
{"name": "DataEvent", "value": 2313},
{"name": "SelectionEvent", "value": 1880},
{"name": "TooltipEvent", "value": 1701},
{"name": "VisualizationEvent", "value": 1117}
]
},
{
"name": "legend",
"children": [
{"name": "Legend", "value": 20859},
{"name": "LegendItem", "value": 4614},
{"name": "LegendRange", "value": 10530}
]
},
{
"name": "operator",
"children": [
{
"name": "distortion",
"children": [
{"name": "BifocalDistortion", "value": 4461},
{"name": "Distortion", "value": 6314},
{"name": "FisheyeDistortion", "value": 3444}
]
},
{
"name": "encoder",
"children": [
{"name": "ColorEncoder", "value": 3179},
{"name": "Encoder", "value": 4060},
{"name": "PropertyEncoder", "value": 4138},
{"name": "ShapeEncoder", "value": 1690},
{"name": "SizeEncoder", "value": 1830}
]
},
{
"name": "filter",
"children": [
{"name": "FisheyeTreeFilter", "value": 5219},
{"name": "GraphDistanceFilter", "value": 3165},
{"name": "VisibilityFilter", "value": 3509}
]
},
{"name": "IOperator", "value": 1286},
{
"name": "label",
"children": [
{"name": "Labeler", "value": 9956},
{"name": "RadialLabeler", "value": 3899},
{"name": "StackedAreaLabeler", "value": 3202}
]
},
{
"name": "layout",
"children": [
{"name": "AxisLayout", "value": 6725},
{"name": "BundledEdgeRouter", "value": 3727},
{"name": "CircleLayout", "value": 9317},
{"name": "CirclePackingLayout", "value": 12003},
{"name": "DendrogramLayout", "value": 4853},
{"name": "ForceDirectedLayout", "value": 8411},
{"name": "IcicleTreeLayout", "value": 4864},
{"name": "IndentedTreeLayout", "value": 3174},
{"name": "Layout", "value": 7881},
{"name": "NodeLinkTreeLayout", "value": 12870},
{"name": "PieLayout", "value": 2728},
{"name": "RadialTreeLayout", "value": 12348},
{"name": "RandomLayout", "value": 870},
{"name": "StackedAreaLayout", "value": 9121},
{"name": "TreeMapLayout", "value": 9191}
]
},
{"name": "Operator", "value": 2490},
{"name": "OperatorList", "value": 5248},
{"name": "OperatorSequence", "value": 4190},
{"name": "OperatorSwitch", "value": 2581},
{"name": "SortOperator", "value": 2023}
]
},
{"name": "Visualization", "value": 16540}
]
}
]
};
function search_and_delete(obj, search_term, parent) {
if (obj.name === search_term) {
parent.splice(parent.indexOf(obj), 1);
} else {
Object.values(obj).forEach((val) => {
if (val && typeof val === "object") {
search_and_delete(val, search_term, obj)
}
});
}
}
search_and_delete(data, "Interpolator")
console.log(data);
关于javascript - 从嵌套 JS 对象中删除其属性后删除空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57539883/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!