Ic3m4n
Ic3m4n

Reputation: 871

Get week in array of days

I have a array of days:

var days = [1,5,7,9,12,16,23,27,45,66];

Now I want to find out which days belong to which week. i.e.

Week 1: 1,5,7
Week 2: 9,12
Week 3: 23,27

and so on.

How can I achieve that?

Upvotes: 0

Views: 67

Answers (2)

Ic3m4n
Ic3m4n

Reputation: 871

deceze's comment was what I was looking for, thank you.

var days = [1, 5, 7, 9, 12, 16, 23, 27, 45, 66];
var weekNumber;

for (i= 0; i <= days.length; i++) {
 weekNumber = Math.ceil(days[i] / 7);

}

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

You could calculate the week with a division by 7 and round up the value to the next integer value.

var days = [1, 5, 7, 9, 12, 16, 23, 27, 45, 66],
    weeks = {};
    
days.forEach(function (d) {
    var w = Math.ceil(d / 7);
    weeks[w] = weeks[w] || [];
    weeks[w].push(d);
});

console.log(weeks);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions