gpt4 book ai didi

javascript - 使用递归转换 json 数据

转载 作者:行者123 更新时间:2023-11-28 01:13:56 24 4
gpt4 key购买 nike

我有一个 json 数据,需要使用 javascript 进行转换。用户使用关键字搜索时,从服务器获取json结果。有些对象为空,有些对象包含搜索结果 json 中的数据。我需要转换成另一个 json,它应该只包含 totalRecords 属性值大于 0 的对象。

我添加了 jsfiddle 的链接以便转换 json。 http://jsfiddle.net/Xhhn4/3/

我尝试使用递归来解决这个问题。其代码如下:

  var resultData=data.results;
var contents = [];
var get_content=function(resultdata){
console.log(resultdata);
$.each(resultdata, function( index, value ) {
value=value || {};
if (value.hasOwnProperty("content") && value.content.length > 0 ) {
contents.push({name:index,value:value});
}else{
get_content(value);

}
});
}

get_content(resultData);
console.log("Printing the content");
console.log(contents);

但是上面的代码会给我一个包含内容的对象数组,但不会给我从根开始的整个树结构。我基本上需要相同格式的整个 json,只删除空对象。我们可以使用递归来实现这一点吗?如何用分治方法来合并divide之后的结果?

之前的 Json:

{
"keywords": [
"keyword1"
],
"results": {
"country": {
"general_search_results": {
"hospitalDetails": {
"totalRecords": 0,
"content": []
},
"schoolsDetails": {
"totalRecords": 0,
"content": []
},
"shoppingMartDetails": {
"totalRecords": 0,
"content": []
}
},
"companies_search_results": {
"totalRecords": 5,
"content": [
{
"companyName": "AAA",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "BBB",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "CCC",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "DDD",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "EEE",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
}
]
},
"vehicles_search_results": {
"twoWheelers": {
"totalRecords": 0,
"content": [],
}
},
"accidents_search_results": {
"totalRecords": 0,
"content": [],
}
},
"state1": {
"general_search_results": {
"hospitalDetails": {
"totalRecords": 0,
"content": []
},
"schoolsDetails": {
"totalRecords": 0,
"content": []
},
"shoppingMartDetails": {
"totalRecords": 0,
"content": []
}
},
"companies_search_results": {
"totalRecords": 5,
"content": [
{
"companyName": "AAA",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "BBB",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "CCC",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "DDD",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "EEE",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
}
]
},
"vehicles_search_results": {
"twoWheelers": {
"totalRecords": 0,
"content": [],
}
},
"accidents_search_results": {
"totalRecords": 0,
"content": [],
}
}
}
}

Json 格式化后:

{
"keywords": [
"keyword1"
],
"results": {
"country": {
"companies_search_results": {
"totalRecords": 5,
"content": [
{
"companyName": "AAA",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "BBB",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "CCC",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "DDD",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "EEE",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
}
]
}
},
"state1": {
"companies_search_results": {
"totalRecords": 5,
"content": [
{
"companyName": "AAA",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "BBB",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "CCC",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "DDD",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "EEE",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
}
]
}
}
}
}

最佳答案

这应该可以解决问题

我将这个函数称为“prune”,因为 prune 的定义意味着 trim 一棵树,使其看起来合适。

const isObject = (o) =>
typeof o == 'object' && o.constructor == Object;

const isEmptyObject = (o) =>
Object.entries(o).length === 0

const hasProperty = (key, o) =>
o.hasOwnProperty(key)

const isPositive = (x) =>
x > 0

const isEmptyArray = (arr) =>
arr.length === 0

const pruneReducer = (obj, [key, value]) => {
if(isObject(value)) {
if(hasProperty("content", value)) {
if(isEmptyArray(value.content)) {
return obj
} else {
obj[key] = value
return obj
}
} else {
const childObj = prune(value)
if(isEmptyObject(childObj)) {
return obj
} else {
obj[key] = childObj
return obj
}
}
} else {
obj[key] = value
return obj
}
}

const prune = (obj) =>
Object.entries(obj).reduce(pruneReducer, {})


const data = {
"keywords": [
"keyword1"
],
"results": {
"country": {
"general_search_results": {
"hospitalDetails": {
"totalRecords": 0,
"content": []
},
"schoolsDetails": {
"totalRecords": 0,
"content": []
},
"shoppingMartDetails": {
"totalRecords": 0,
"content": []
}
},
"companies_search_results": {
"totalRecords": 5,
"content": [
{
"companyName": "AAA",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "BBB",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "CCC",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "DDD",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "EEE",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
}
]
},
"vehicles_search_results": {
"twoWheelers": {
"totalRecords": 0,
"content": [],
}
},
"accidents_search_results": {
"totalRecords": 0,
"content": [],
}
},
"state1": {
"general_search_results": {
"hospitalDetails": {
"totalRecords": 0,
"content": []
},
"schoolsDetails": {
"totalRecords": 0,
"content": []
},
"shoppingMartDetails": {
"totalRecords": 0,
"content": []
}
},
"companies_search_results": {
"totalRecords": 5,
"content": [
{
"companyName": "AAA",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "BBB",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "CCC",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "DDD",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
},
{
"companyName": "EEE",
"location": "bangalore",
"employees": "2000",
"reputation": 4,
}
]
},
"vehicles_search_results": {
"twoWheelers": {
"totalRecords": 0,
"content": [],
}
},
"accidents_search_results": {
"totalRecords": 0,
"content": [],
}
}
}
}

console.log(prune(data))

关于javascript - 使用递归转换 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24079663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com