1

I'm requesting a list of jobs as JSON (complex object of objects & arrays), at a set interval, using AJAX. I'm then updating parts of a page using the returned data.

The issue is that I only want to update the page with any new jobs and I can't compare the number of objects because of how other things are implemented.

How can I only return only new object(s) by comparing oldData & newData?

Example JSON object:

{
    latStamp: 32.6132698059082,
    timeStamp: "2014-06-20 07:05:00+00:00",
    job: {
        status: 8,
        customer: "Mr Crustomer",
        task: "Some task",
        machineType: "XE3000",
        tempJobNumber: "GO28323",
        jobNumber: "GD23203",
        causeOfFault: "",
        workCarriedOut: "",
        jobOwnerName: "Job Owner",
        machineSerialNumber: "",
        costCentre: 1,
        mileage: 0,
        location: "Location",
        scheduledDateTime: "19/06/2014 17:00:00",
        shortDescription: "",
        id: 1,
        engineer: 1
    },
    status: {
        actionGuidance: "Action guidance here",
        description: "Completed",
        endsTimer: true,
        colour: "73B63F",
        startsTimer: false,
        id: 8
    },
    lngStamp: 4.886201977729797
},

Edit: I don't think this is a duplicate, as suggested link returns an object containing multiple empty objects – I'm looking to return an object containing only the different objects

chridam
  • 100,957
  • 23
  • 236
  • 235
Dan
  • 53
  • 4

1 Answers1

0

My understanding of the question is you have to find out new jobs by comparing two different json one is oldData and other is newData i assume the timestamps of new jobs in newData json should be greater than any of the old jobs in OldData.

Having said that you can do the following

1) find the job of latest timestamp ,means the latest job 2) save its timestamp in a variable as latestJobTime 3) iterate all the jobs in the newData and find jobs which has timestamps after latestJobTime. 4) update latestJobTime with the time of latest Job. do similar steps when newData comes again.

some tips to save timestamp.

var latestJobTime = new Date(newData['timeStamp']).getTime(); // gives you the millisecond representation of time so that you can compare with number comparisons.

jsHero
  • 180
  • 2
  • 14