-3

I have a data structure like this:

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

My requirement is, I need to add Job Details under MADRAS to Tamil Nadu and I need to sort the data based on one property -PostedDate. So my result should be something like,

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

I tried to extract Madras data and add that to under Tamil Nadu. But nothing is working. I know how to extract single or multiple value from JSON object. But I need to edit that JSON and sort it. That I am able to do it.

Mahesh Narayanan
  • 123
  • 5
  • 21
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Liam Sep 12 '17 at 09:25
  • I know how to extract data using property, but here i need to extract data from one property and add it under another property. Then I need to sort it. – Mahesh Narayanan Sep 12 '17 at 09:32

2 Answers2

0

I got the solution. When the countryName is "Tamil Nadu" and "MADRAS",I extracted all the data and saved it in a new array using below code.

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

}

}

Here newTmpRecord is an Array and is like universal variable

For sorting I used below codes

    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
    });
}
Mahesh Narayanan
  • 123
  • 5
  • 21
-2

You can simply extract the object "Madras" from the array and add all of its Jobdetails to the object "Tamil Nadu" in a for loop. You can either look where to add them in the loop by checking the dates, or you can write a sort function, which is pretty easy in javascript and well explained here:

You might want to look up objects

And here the sorting is explained.

Absent
  • 884
  • 3
  • 11
  • 24