Javascript 从JSON中删除属性
我使用D3创建了一个JSON树结构,如下所示。我正在保存到Mongo DB中,但我正在尝试删除不必要的字段,如Javascript 从JSON中删除属性,javascript,json,Javascript,Json,我使用D3创建了一个JSON树结构,如下所示。我正在保存到Mongo DB中,但我正在尝试删除不必要的字段,如x、y、id、xo和yo。我想知道是否有任何自然的d3.js函数可以做到这一点 { "name": "law", "children": [ { "name": "criminal", "children": [ { "name": "drugs",
x
、y
、id
、xo
和yo
。我想知道是否有任何自然的d3.js函数可以做到这一点
{
"name": "law",
"children": [
{
"name": "criminal",
"children": [
{
"name": "drugs",
"children": [
{
"name": "abuse",
"depth": 3,
"x": 14.5161295,
"y": 870,
"id": 17,
"x0": 14.5161295,
"y0": 870,
"children": [
{
"name": "asd",
"id": "aa909548-9200-45d6-86e9-f02708bfb70d",
"depth": 4,
"_children": null,
"x": 14.5161295,
"y": 1160,
"x0": 14.5161295,
"y0": 1160
}
]
},
{
"name": "trafficking",
"depth": 3,
"x": 29.032259,
"y": 870,
"id": 18,
"x0": 29.032259,
"y0": 870
}
],
"depth": 2,
"x": 21.774193,
"y": 580,
"id": 19,
"x0": 21.774193,
"y0": 580
},
{
"name": "sexual harrasement",
"children": [
{
"name": "harrasement in education",
"depth": 3,
"x": 58.064518,
"y": 870,
"id": 15,
"x0": 58.064518,
"y0": 870
},
{
"name": "harrasement in workplace",
"depth": 3,
"x": 72.58064,
"y": 870,
"id": 14,
"x0": 72.58064,
"y0": 870
}
],
"depth": 2,
"x": 65.32258,
"y": 580,
"id": 16,
"x0": 65.32258,
"y0": 580
},
{
"name": "violent",
"children": [
{
"name": "acts intended to cause injury",
"depth": 3,
"x": 101.6129,
"y": 870,
"id": 12,
"x0": 101.6129,
"y0": 870
},
{
"name": "robbery",
"depth": 3,
"x": 116.129036,
"y": 870,
"id": 11,
"x0": 116.129036,
"y0": 870
},
{
"name": "sexual assault",
"depth": 3,
"x": 130.64516,
"y": 870,
"id": 10,
"x0": 130.64516,
"y0": 870
}
],
"depth": 2,
"x": 116.129036,
"y": 580,
"id": 13,
"x0": 116.129036,
"y0": 580
}
],
"depth": 1,
"x": 68.951614,
"y": 290,
"id": 20,
"x0": 68.951614,
"y0": 290
},
{
"name": "family",
"children": [
{
"name": "adoption",
"depth": 2,
"x": 152.41936,
"y": 580,
"id": 2,
"x0": 152.41936,
"y0": 580
},
{
"name": "child abuse",
"depth": 2,
"x": 166.93549,
"y": 580,
"id": 1,
"x0": 166.93549,
"y0": 580
},
{
"name": "divorce",
"children": [
{
"name": "contested-divorce",
"children": [
{
"name": "mediation",
"depth": 4,
"x": 152.41936,
"y": 1160,
"id": 6,
"x0": 152.41936,
"y0": 1160
},
{
"id": "2.1.1.1",
"name": "trial",
"depth": 4,
"x": 166.93549,
"y": 1160,
"x0": 166.93549,
"y0": 1160
}
],
"depth": 3,
"x": 159.67741,
"y": 870,
"id": 7,
"x0": 159.67741,
"y0": 870
},
{
"name": "no-fault divorce",
"children": [
{
"name": "custody",
"depth": 4,
"x": 195.96774,
"y": 1160,
"id": 4,
"x0": 195.96774,
"y0": 1160
},
{
"name": "division of marital assets",
"depth": 4,
"x": 210.48387,
"y": 1160,
"id": 3,
"x0": 210.48387,
"y0": 1160
}
],
"depth": 3,
"x": 203.2258,
"y": 870,
"id": 5,
"x0": 203.2258,
"y0": 870
}
],
"depth": 2,
"x": 181.45161,
"y": 580,
"id": 8,
"x0": 181.45161,
"y0": 580
}
],
"depth": 1,
"x": 166.93549,
"y": 290,
"id": 9,
"x0": 166.93549,
"y0": 290
}
],
"x0": 117.94355,
"y0": 0,
"depth": 0,
"x": 117.94355,
"y": 0,
"id": 21
}
首先,我完全同意Gerardo的说法,这与D3无关,也不是解决这个问题的一个很好的选择。事实证明,这可以用纯JavaScript轻松完成。第二,虽然您的问题不太清楚,但我假设您处理的对象是文字符号,而不是JSON字符串 基本上有两种方法可以做到这一点:
JSON.stringify()
的第二个参数提供:
[……]
替换件|可选
一种改变字符串化过程行为的函数,或字符串和数字对象数组,用作选择/过滤要包含在JSON字符串中的值对象属性的白名单。如果此值为null或未提供,则生成的JSON字符串中包含对象的所有属性
但是,要使其正常工作,需要指定要包含在输出中的属性的白名单。请注意,这与选项1的删除相反
对于您的情况,可能是这样的:
JSON.stringify(data, ["name", "depth", "children"]);
var数据={
“名称”:“法律”,
“儿童”:[
{
“姓名”:“罪犯”,
“儿童”:[
{
“名称”:“药物”,
“儿童”:[
{
“姓名”:“虐待”,
“深度”:3,
“x”:14.5161295,
“y”:870,
“id”:17,
“x0”:14.5161295,
“y0”:870,
“儿童”:[
{
“名称”:“asd”,
“id”:“aa909548-9200-45d6-86e9-f02708bfb70d”,
“深度”:4,
“_children”:空,
“x”:14.5161295,
“y”:1160,
“x0”:14.5161295,
“y0”:1160
}
]
},
{
“姓名”:“贩运”,
“深度”:3,
“x”:29.032259,
“y”:870,
“id”:18,
“x0”:29.032259,
“y0”:870
}
],
“深度”:2,
“x”:21.774193,
“y”:580,
“id”:19,
“x0”:21.774193,
“y0”:580
},
{
“姓名”:“性骚扰”,
“儿童”:[
{
“名称”:“教育中的哈拉塞特”,
“深度”:3,
“x”:58.064518,
“y”:870,
“id”:15,
“x0”:58.064518,
“y0”:870
},
{
“名称”:“工作场所的哈拉塞特”,
“深度”:3,
“x”:72.58064,
“y”:870,
“id”:14,
“x0”:72.58064,
“y0”:870
}
],
“深度”:2,
“x”:65.32258,
“y”:580,
“id”:16,
“x0”:65.32258,
“y0”:580
},
{
“姓名”:“暴力”,
“儿童”:[
{
“名称”:“意图造成伤害的行为”,
“深度”:3,
“x”:101.6129,
“y”:870,
“id”:12,
“x0”:101.6129,
“y0”:870
},
{
“姓名”:“抢劫”,
“深度”:3,
“x”:116.129036,
“y”:870,
“id”:11,
“x0”:116.129036,
“y0”:870
},
{
“姓名”:“性侵犯”,
“深度”:3,
“x”:130.64516,
“y”:870,
“id”:10,
“x0”:130.64516,
“y0”:870
}
],
“深度”:2,
“x”:116.129036,
“y”:580,
“id”:13,
“x0”:116.129036,
“y0”:580
}
],
“深度”:1,
“x”:68.951614,
“y”:290,
“id”:20,
“x0”:68.951614,
“y0”:290
},
{
“姓名”:“家庭”,
“儿童”:[
{
“姓名”:“收养”,
“深度”:2,
“x”:152.41936,
“y”:580,
“id”:2,
“x0”:152.41936,
“y0”:580
},
{
“姓名”:“虐待儿童”,
“深度”:2,
“x”:166.93549,
“y”:580,
“id”:1,
JSON.stringify(data, ["name", "depth", "children"]);