在嵌套数据结构中编辑对象

时间:2017-09-12 09:24:27

标签: javascript

我有这样的数据结构:

var fieldTmp= [{
"CountryDetails":[{
    "countryName":"Kerala",
    "JobDetails":[{
        "RequisitionId":"00020447961",
        "City":"KOCHI",
        "PostedDate":"2016-12-18"
        },{
        "RequisitionId":"26103",
        "City":"TRIVANDRUM",
        "PostedDate":"2016-12-12"
        },{
        "RequisitionId":"26077",
        "City":"ALAPPEY",
        "PostedDate":"2016-10-09"
        },{
        "RequisitionId":"00020774701",
        "City":"KOTTAYAM",
        "PostedDate":"2016-06-12"
        },{
        "RequisitionId":"26078",
        "City":"ADOOR",
        "PostedDate":"2016-05-19"}]
    },
    "countryName":"MADRAS",
    "JobDetails":[{
        "RequisitionId":"0025456",
        "City":"CHENNAI",
        "PostedDate":"2017-06-05"
        },{
        "RequisitionId":"69847562",
        "City":"ADYAR",
        "PostedDate":"2016-10-14"}]
    },
    {"countryName":"Tamil Nadu",
    "JobDetails":[{
        "RequisitionId":"00020550501",
        "City":"CHENNAI",
        "PostedDate":"2016-12-18"
        },{
        "RequisitionId":"00020786022",
        "City":"KOVAI",
        "PostedDate":"2016-09-01"
        },{
        "RequisitionId":"00020786071",
        "City":"TRICHY",
        "PostedDate":"2016-04-10"}]
}] }]

我的要求是,我需要在MADRAS下向Tamil Nadu添加作业详细信息,我需要根据一个属性-PostedDate对数据进行排序。 所以我的结果应该是这样的,

var fieldTmp= [{
"CountryDetails":[{
    "countryName":"Kerala",
    "JobDetails":[{
        "RequisitionId":"00020447961",
        "City":"KOCHI",
        "PostedDate":"2016-12-18"
        },{
        "RequisitionId":"26103",
        "City":"TRIVANDRUM",
        "PostedDate":"2016-12-12"
        },{
        "RequisitionId":"26077",
        "City":"ALAPPEY",
        "PostedDate":"2016-10-09"
        },{
        "RequisitionId":"00020774701",
        "City":"KOTTAYAM",
        "PostedDate":"2016-06-12"
        },{
        "RequisitionId":"26078",
        "City":"ADOOR",
        "PostedDate":"2016-05-19"}]
    },
    {"countryName":"Tamil Nadu",
    "JobDetails":[{
        "RequisitionId":"0025456",
        "City":"CHENNAI",
        "PostedDate":"2017-06-05"
        },{
        "RequisitionId":"00020550501",
        "City":"CHENNAI",
        "PostedDate":"2016-12-18"
        },{
        "RequisitionId":"69847562",
        "City":"ADYAR",
        "PostedDate":"2016-10-14"
        },{
        "RequisitionId":"00020786022",
        "City":"KOVAI",
        "PostedDate":"2016-09-01"
        },{
        "RequisitionId":"00020786071",
        "City":"TRICHY",
        "PostedDate":"2016-04-10"}]
}] }]

我试图提取Madras数据并将其添加到Tamil Nadu下。但没有任何工作。 我知道如何从JSON对象中提取单个或多个值。但我需要编辑JSON并对其进行排序。我能够做到。

2 个答案:

答案 0 :(得分:0)

我得到了解决方案。 当 countryName 是“Tamil Nadu”和“MADRAS”时,我提取了所有数据并使用下面的代码将其保存在新数组中。

function mergingBothStateDetails(jsonJobDetails){
for(var j=0;j<jsonJobDetails.length;j++)
{
    newTmpRecord.push({"RequisitionId":jsonJobDetails[j].RequisitionId,
                       "PostedDate":jsonJobDetails[j].PostedDate,
                       "City":jsonJobDetails[j].City});

}

}

这里 newTmpRecord 是一个数组,就像通用变量

对于排序我使用下面的代码

    function sortNewList(){
    newTmpRecord.sort(function(a, b){ // sort object by retirement date
        var dateA=new Date(a.PostedDate), dateB=new Date(b.PostedDate)
        return dateB-dateA //sort by date descending
    });
}

答案 1 :(得分:-2)

您可以简单地提取对象&#34; Madras&#34;从数组中将所有Jobdetails添加到对象&#34; Tamil Nadu&#34;在for循环中。您可以通过检查日期来查看在循环中添加它们的位置,或者您可以编写一个排序函数,这在javascript中非常简单,并在此处进行了详细解释:

You might want to look up objects

And here the sorting is explained.