从JSON中删除属性

时间:2017-05-10 09:39:28

标签: javascript json

我使用D3有一个类似下面的JSON树结构。我正在保存到Mongo数据库,但我正在尝试删除xyidxoyo等不必要的字段。我想知道是否有任何使用任何自然的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
}

1 个答案:

答案 0 :(得分:1)

首先,我完全赞同Gerardo的comment声明这与D3中的特殊情况无关,也不是D3是解决这个问题的一个非常好的选择。事实证明,这可以在纯JavaScript中轻松完成。其次,虽然从你的问题中并不完全清楚,但我认为你是用文字符号而不是JSON字符串处理对象。

基本上有两种方法可以完成这项工作:

  1. 使用delete运算符递归遍历树,以消除您在保存的结果JSON字符串中所需的属性。有很多种方法可以进行实际的递归,其中很多都被类似的问题广泛涵盖,所以我将它放在一边。

  2. 由于您明确要求将树存储为JSON,因此可以使用replacer参数,该参数可作为JSON.stringify()的第二个参数提供:

    JSON.stringify(value[, replacer[, space]])
    
         

    [...]

         

    替换者 | 可选

         

    一个改变字符串化过程行为的函数,或一个String和Number对象数组,用作选择/过滤要包含在JSON中的值对象的属性的白名单串。如果此值为null或未提供,则对象的所有属性都包含在生成的JSON字符串中。

    但是,要使其正常工作,您需要指定要包含在输出中的属性的白名单。请注意,这与删除选项1相反。

    对于你的情况,这可能是这样的:

    JSON.stringify(data, ["name", "depth", "children"]);
    

    
    
    var data = {
       "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
    };
    
    var json = JSON.stringify(data, ["name", "depth", "children"]);
    console.log(json);
    
    
    

相关问题