如何从嵌套JSON递归删除所有空数组childs []

时间:2019-11-29 17:40:32

标签: javascript jquery json lodash

我有一个如下的JSON响应:我正在使用来自GeoRegionCountries APIController的嵌套JSON数据和自定义类TreeView根据我使用的插件所需的嵌套结构来格式化数据。我正在使用使用此jquery插件的Multi-Select Drop Down Tree插件组合多选Treeview,您可以通过此链接jquery plugin Multi-Select Drop Down Tree Plugin

看到它
[
          {
            "Id": 1,
            "Title": "United States",
            "ParentId": null,
            "Subs": [
              {
                "Id": 7,
                "Title": "Northwest",
                "ParentId": 1,
                "Subs": []
              },
              {
                "Id": 8,
                "Title": "Northeast",
                "ParentId": 1,
                "Subs": []
              },
              {
                "Id": 9,
                "Title": "Central",
                "ParentId": 1,
                "Subs": []
              },
              {
                "Id": 10,
                "Title": "Southwest",
                "ParentId": 1,
                "Subs": []
              },
              {
                "Id": 18,
                "Title": "Southeast",
                "ParentId": 1,
                "Subs": []
              }
            ]
          },
          {
            "Id": 2,
            "Title": "Canada",
            "ParentId": null,
            "Subs": []
          },
          {
            "Id": 3,
            "Title": "France",
            "ParentId": null,
            "Subs": []
          },
          {
            "Id": 4,
            "Title": "Germany",
            "ParentId": null,
            "Subs": []
          },
          {
            "Id": 5,
            "Title": "Australia",
            "ParentId": null,
            "Subs": []
          },
          {
            "Id": 6,
            "Title": "United Kingdom",
            "ParentId": null,
            "Subs": []
          }
        ]

我要删除所有带有空数组的“ Subs”。

         [
          {
            "Id": 1,
            "Title": "United States",
            "ParentId": null,
            "Subs": [
              {
                "Id": 7,
                "Title": "Northwest",
                "ParentId": 1
              },
              {
                "Id": 8,
                "Title": "Northeast",
                "ParentId": 1
              },
              {
                "Id": 9,
                "Title": "Central",
                "ParentId": 1
              },
              {
                "Id": 10,
                "Title": "Southwest",
                "ParentId": 1
              },
              {
                "Id": 18,
                "Title": "Southeast",
                "ParentId": 1
              }
            ]
          },
          {
            "Id": 2,
            "Title": "Canada",
            "ParentId": null
          },
          {
            "Id": 3,
            "Title": "France",
            "ParentId": null
          },
          {
            "Id": 4,
            "Title": "Germany",
            "ParentId": null
          },
          {
            "Id": 5,
            "Title": "Australia",
            "ParentId": null
          },
          {
            "Id": 6,
            "Title": "United Kingdom",
            "ParentId": null
          }
        ]

深层清洁的最佳方法是什么?我在Stackopverflow中尝试了不同的解决方案,但我得到的只是对象对象,而不是空的Subs-我不想要。

      [
          {
            "Id": 1,
            "Title": "United States",
            "ParentId": null,
            "Subs": [
              {
                "Id": 7,
                "Title": "Northwest",
                "ParentId": 1,
                Object object
              },
              {
                "Id": 8,
                "Title": "Northeast",
                "ParentId": 1,
                Object object
              },
              {
                "Id": 9,
                "Title": "Central",
                "ParentId": 1,
                Object object
              },
              {
                "Id": 10,
                "Title": "Southwest",
                "ParentId": 1,
                Object object
              },
              {
                "Id": 18,
                "Title": "Southeast",
                "ParentId": 1,
                Object object
              }
            ]
          },
          {
            "Id": 2,
            "Title": "Canada",
            "ParentId": null,
            Object object
          },
          {
            "Id": 3,
            "Title": "France",
            "ParentId": null,
            Object object
          },
          {
            "Id": 4,
            "Title": "Germany",
            "ParentId": null,
            Object object
          },
          {
            "Id": 5,
            "Title": "Australia",
            "ParentId": null,
            Object object
          },
          {
            "Id": 6,
            "Title": "United Kingdom",
            "ParentId": null,
            Object object
          }
        ]

这不是我想要的

4 个答案:

答案 0 :(得分:2)

您可以使用_.transform()来递归检查特定键(Subs),如果其值为空,则将其删除:

const { transform, isObject, isEmpty } = _;

const removeEmpty = (obj, key) =>
  transform(obj, (r, v, k) => {
    if(k === key && isEmpty(v)) return;
    r[k] = isObject(v) ? removeEmpty(v, key) : v;
  });

const tree = [{"Id":1,"Title":"United States","ParentId":null,"Subs":[{"Id":7,"Title":"Northwest","ParentId":1,"Subs":[]},{"Id":8,"Title":"Northeast","ParentId":1,"Subs":[]},{"Id":9,"Title":"Central","ParentId":1,"Subs":[]},{"Id":10,"Title":"Southwest","ParentId":1,"Subs":[]},{"Id":18,"Title":"Southeast","ParentId":1,"Subs":[]}]},{"Id":2,"Title":"Canada","ParentId":null,"Subs":[]},{"Id":3,"Title":"France","ParentId":null,"Subs":[]},{"Id":4,"Title":"Germany","ParentId":null,"Subs":[]},{"Id":5,"Title":"Australia","ParentId":null,"Subs":[]},{"Id":6,"Title":"United Kingdom","ParentId":null,"Subs":[]}]

const result = removeEmpty(tree, 'Subs');

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

答案 1 :(得分:0)

正确的答案是这样的:

let array = 	[
		{
			'Id': 1,
			'Title': 'United States',
			'ParentId': null,
			'Subs': [
				{
					'Id': 7,
					'Title': 'Northwest',
					'ParentId': 1,
					'Subs': []
				},
				{
					'Id': 8,
					'Title': 'Northeast',
					'ParentId': 1,
					'Subs': []
				},
				{
					'Id': 9,
					'Title': 'Central',
					'ParentId': 1,
					'Subs': []
				},
				{
					'Id': 10,
					'Title': 'Southwest',
					'ParentId': 1,
					'Subs': []
				},
				{
					'Id': 18,
					'Title': 'Southeast',
					'ParentId': 1,
					'Subs': []
				}
			]
		},
		{
			'Id': 2,
			'Title': 'Canada',
			'ParentId': null,
			'Subs': []
		},
		{
			'Id': 3,
			'Title': 'France',
			'ParentId': null,
			'Subs': []
		},
		{
			'Id': 4,
			'Title': 'Germany',
			'ParentId': null,
			'Subs': []
		},
		{
			'Id': 5,
			'Title': 'Australia',
			'ParentId': null,
			'Subs': []
		},
		{
			'Id': 6,
			'Title': 'United Kingdom',
			'ParentId': null,
			'Subs': []
		}
	]
let newArray = array.map(item=> {
	if (item.Subs.length===0){
		delete item.Subs
		return item
	}
	item.Subs = item.Subs.map(item=>{
		if (item.Subs.length===0){
			delete item.Subs
			return item
		}
	})
	return item
}
)
console.log(newArray)

答案 2 :(得分:0)

let data = [
          {
            "Id": 1,
            "Title": "United States",
            "ParentId": null,
            "Subs": [
              {
                "Id": 7,
                "Title": "Northwest",
                "ParentId": 1,
                "Subs": []
              },
              {
                "Id": 8,
                "Title": "Northeast",
                "ParentId": 1,
                "Subs": []
              },
              {
                "Id": 9,
                "Title": "Central",
                "ParentId": 1,
                "Subs": []
              },
              {
                "Id": 10,
                "Title": "Southwest",
                "ParentId": 1,
                "Subs": []
              },
              {
                "Id": 18,
                "Title": "Southeast",
                "ParentId": 1,
                "Subs": []
              }
            ]
          },
          {
            "Id": 2,
            "Title": "Canada",
            "ParentId": null,
            "Subs": []
          },
          {
            "Id": 3,
            "Title": "France",
            "ParentId": null,
            "Subs": []
          },
          {
            "Id": 4,
            "Title": "Germany",
            "ParentId": null,
            "Subs": []
          },
          {
            "Id": 5,
            "Title": "Australia",
            "ParentId": null,
            "Subs": []
          },
          {
            "Id": 6,
            "Title": "United Kingdom",
            "ParentId": null,
            "Subs": []
          }
        ];

data = data.map(row=>{
  if (!row.Subs.length) {
     let {Subs,...r} = row;
     return r;
  } return row
})

console.log(data);

答案 3 :(得分:0)

编写两个函数,并将遍历数组的函数传递给数据的map函数,如下所示

function formatData(val) {
  if (val.Subs.length > 0) val.Subs.map(a => a.Subs.length > 0 ? formatData(a.Subs) : deleteSubs(a));
  else deleteSubs(val);
  return val;
}

function deleteSubs(val) {
  delete val.Subs;
}

var data = [{
    "Id": 1,
    "Title": "United States",
    "ParentId": null,
    "Subs": [{
        "Id": 7,
        "Title": "Northwest",
        "ParentId": 1,
        "Subs": []
      },
      {
        "Id": 8,
        "Title": "Northeast",
        "ParentId": 1,
        "Subs": []
      },
      {
        "Id": 9,
        "Title": "Central",
        "ParentId": 1,
        "Subs": []
      },
      {
        "Id": 10,
        "Title": "Southwest",
        "ParentId": 1,
        "Subs": []
      },
      {
        "Id": 18,
        "Title": "Southeast",
        "ParentId": 1,
        "Subs": []
      }
    ]
  },
  {
    "Id": 2,
    "Title": "Canada",
    "ParentId": null,
    "Subs": []
  },
  {
    "Id": 3,
    "Title": "France",
    "ParentId": null,
    "Subs": []
  },
  {
    "Id": 4,
    "Title": "Germany",
    "ParentId": null,
    "Subs": []
  },
  {
    "Id": 5,
    "Title": "Australia",
    "ParentId": null,
    "Subs": []
  },
  {
    "Id": 6,
    "Title": "United Kingdom",
    "ParentId": null,
    "Subs": []
  }
]
console.log(data.map(formatData))