skip to Main Content

I’m trying to filter out objects based on values in two arrays, but it keeps returning incorrect ones/too many.

Scenario:

I have an array of evidences and an array of assessments, which have to be filtered out by their dates. F.e. only evidences that are between assessment’s startDate and endDate have to be returned in a new array. In my example below I keep returning all evidences, even though I ask to compare uploadDate between startDate and endDate:

let evidences = [
    { id: 'ev1', name: 'evidence 1', uploadDate: new Date('2023-04-28') },
    { id: 'ev2', name: 'evidence 2', uploadDate: new Date('2023-05-02') },
];

let assessments = [
    { id: 'ca1', name: 'assessment 1', startDate: new Date('2023-04-25'), endDate: new Date('2023-05-01') },
    { id: 'ca2', name: 'assessment 2', startDate: new Date('2023-05-02'), endDate: new Date('2023-05-08') },
];

let evidencesPerAssessment = [];

for (var i = 0; i < assessments.length; i++) {
    var startDate = assessments[i].startDate;
    var endDate = assessments[i].endDate;

    for (var j = 0; j < evidences.length; j++) {
        var uploadDate = evidences[j].uploadDate;

        if (uploadDate >= startDate && uploadDate <= endDate) {
            evidencesPerAssessment.push(evidences[j]);
        }
    }
}

console.log(evidencesPerAssessment);

What I want to achieve, is that for each assessment only evidences that are between its startDate and endDate would be pushed to the array. In this case with test data only ‘evidence 1’ should be shown for ‘assessment 1’ as that evidence’s uploadDate (2023-04-28) is between startDate and endDate (2023-04-25 – 2023-05-01).

Does anyone have an idea on how to achieve this?

2

Answers


  1. You need to make a few alterations to the flow of the code.

    You need to distinguish between assessments, and this needs to be done inside the assessment loop, by creating a new array.

    for (var i = 0; i < assessments.length; i++) {
        let evidencesPerAssessment = [];
    
    let evidences = [
        { id: 'ev1', name: 'evidence 1', uploadDate: new Date('2023-04-28') },
        { id: 'ev2', name: 'evidence 2', uploadDate: new Date('2023-05-02') },
    ];
    
    let assessments = [
        { id: 'ca1', name: 'assessment 1', startDate: new Date('2023-04-25'), endDate: new Date('2023-05-01') },
        { id: 'ca2', name: 'assessment 2', startDate: new Date('2023-05-02'), endDate: new Date('2023-05-08') },
    ];
    
    for (var i = 0; i < assessments.length; i++) {
        let evidencesPerAssessment = [];
        console.log(assessments[i].name);
        var startDate = assessments[i].startDate;
        var endDate = assessments[i].endDate;
    
        for (var j = 0; j < evidences.length; j++) {
            var uploadDate = evidences[j].uploadDate;
    
            if (uploadDate >= startDate && uploadDate <= endDate) {
                evidencesPerAssessment.push(evidences[j]);
            }
        }
            console.log(evidencesPerAssessment);
     }
    Login or Signup to reply.
  2. The code you have written is working fine but you are pushing the values to a single array evidencesPerAssessment.Instead you push it to an array that is specific to a assessment.And can insert the array to the evidencesPerAssessment.

    let evidences = [
        { id: 'ev1', name: 'evidence 1', uploadDate: new Date('2023-04-28') },
        { id: 'ev2', name: 'evidence 2', uploadDate: new Date('2023-05-02') },
    ];
    
    let assessments = [
        { id: 'ca1', name: 'assessment 1', startDate: new Date('2023-04-25'), endDate: new Date('2023-05-01') },
        { id: 'ca2', name: 'assessment 2', startDate: new Date('2023-05-02'), endDate: new Date('2023-05-08') },
    ];
    
    let evidencesPerAssessment = [];
    
    for (var i = 0; i < assessments.length; i++) {
        var startDate = assessments[i].startDate;
        var endDate = assessments[i].endDate;
            let myarr=[]
        for (var j = 0; j < evidences.length; j++) {
            var uploadDate = evidences[j].uploadDate;
    
            if (uploadDate >= startDate && uploadDate <= endDate) {
                myarr.push(evidences[j]);
            }
        }
        evidencesPerAssessment.push(myarr);
    }
    
    console.log(evidencesPerAssessment);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search