使用递归转换json数据

时间:2014-06-06 10:30:23

标签: javascript recursion

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

我正在为要转换的json添加jsfiddle的链接。 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采用相同的格式,只删除空对象。我们可以使用递归以及如何在dividedivide and conquer方法之后合并结果来实现此目的吗?

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,
          }
        ]
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题

我将函数称为“修剪”是因为修剪的定义意味着修剪一棵树,使其看起来合适。

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))