1

I have a large database of items that have somewhat fluid statuses. I need to get an array of those items based on what each items's status was on a given date.

Here's an excerpt from an example record:

{"status":[
  {"date":{"$date":"2019-06-14T06:17:41.625Z"},"statusCode":200},
  {"date":{"$date":"2019-11-04T02:02:58.020Z"},"statusCode":404},
  {"date":{"$date":"2020-08-07T01:11:16.184Z"},"statusCode":200},
  {"date":{"$date":"2020-08-07T03:54:09.703Z"},"statusCode":404}
]}

Using this example, the status on 2020-01-13 would be 404 (as it would be also on 2020-01-12 or any other givenDate until the status changed back to 200).

So how would I filter my big array to this record (and others like it) to only items with status 404 as of 2020-01-13? (And I would do the same for 200.)

Note that I can't simply filter for objects with date < givenDate && statusCode == 200 because that would ignore if the status changed after those records. (The above example would return for either 200 or 404 since both records exist before givenDate.)

My only idea at the moment is that I could first filter the status array to anything before givenDate, and then compare based on the last record (since this filtered array's last record would then always be before givenDate). But this seems more complicated than necessary.

Processing time isn't important to me on this because I'm trying to make some one-time corrections to past statistics.

Thanks in advance!

1 Answer 1

1

A bit verbose, but I think this should do what you want.

var feedHistory = {"status":[
  {"date":{"$date":"2019-06-14T06:17:41.625Z"},"statusCode":200},
  {"date":{"$date":"2019-11-04T02:02:58.020Z"},"statusCode":404},
  {"date":{"$date":"2020-08-07T01:11:16.184Z"},"statusCode":200},
  {"date":{"$date":"2020-08-07T03:54:09.703Z"},"statusCode":404}
]};

const filterByStatus = (feedHistory,statusDate) => {
    let foundRecord = false;
    feedHistory.forEach((record) => {
        let recordDate = new Date(Date.parse(record.date['$date']));
        
        if (recordDate < statusDate && (!foundRecord || foundRecord.parsedDate < recordDate)) {
            record.parsedDate = recordDate;
            foundRecord = record;
        }
    });
    return foundRecord;
};

var statusDate = new Date('2019-06-15');

var statusOnDate = filterByStatus(feedHistory.status,statusDate);
console.log(`On ${statusDate} the status was ${statusOnDate.statusCode}`);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Unfortunately, that seems to be carrying over the first found status. So any statusDate I enter returns 200, even if after the last status. But I think this gives me something to work with a little.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.