0

I have three arrays:

var dates = ['07/31/2013', '08/01/2013', '08/02/2013', '08/03/2013', '08/04/2013', '08/05/2013', '08/06/2013', '08/07/2013'];

The dates array contains every date in a range of a week.

var meetingdates = ['07/31/2013', '08/02/2013', '08/03/2013', '08/04/2013', '08/05/2013', '08/07/2013'];

The meetingdates array contains dates, where there are 1 or more meetings.

var meetings = ['1', '3', '2', '4', '1', '5']

The meetings array contains the number of the meetings on the dates from meetingdates.

I want to compare the dates array with the meetings array, if an entry from the meetingsarray exists in dates I want to put the number of the meetings which belongs to that date in a new array, if theres no match an 0 will be inserted:

So after comparing the meetingsarray with dates this will be the final result.

 meetingsPerDate =  ['1', '0', '3', '2', '4', '1', '0', '0'];

I tried solving this with the following for-loop:

if(dates[y] == meetingdates [y]){
    meetingsPerDate [y] = meetings [y];
    }else {
         meetingsPerDate[y] = 0;
          }  
     }

I know I will be missing some numbers because both of the arrays are not following the same pattern, but I really don't know how to solve this.

I hope I've explained my problem well, if not please let me know.

Any help is very appreciated,

Thanks!

2 Answers 2

1

Use an object instead of an array:

var meetings = {
    '07/31/2013': 1,
    '08/02/2013': 3,
    '08/03/2013': 2,
    '08/04/2013': 4,
    '08/05/2013': 1,
    '08/07/2013': 5
}

And then just add the new dates into it:

var date;

for (var i = 0; i < dates.length; i++) {
    date = dates[i];

    if (date in meetings) {
        meetings[date] += 1;
    } else {
        meetings[date] = 1;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your input! But I managed to find the solution myself.
0

Not sure if it's the best solution. But I managed to find the answer to my problem:

for (var i = 0; i < dates.length; i++) {
    var arrayIndex = meetingdates.indexOf(dates[i]);

    if (arrayIndex == -1) {
        console.log(dates[i] + ' ' + 0);
    } else {
        console.log(dates[i] + ' ' + meetings[arrayIndex]);
  }

This small loop returns exactly what I needed.

Comments

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.