Reputation: 1598
hi guys i'm struggle to understand how can i solve something.
i have 7 dates , that i have to add for each date six different value of time slot number , the results need to look this.
{
"dateTime": "2017-05-25T09:20:58.846Z",
"timeSlotArray":
{
"available": true,
"placeAfter": "string",
"placeBefore": "string",
"slotNumber": 0
}
}
{
"dateTime": "2017-05-25T09:20:58.846Z",
"timeSlotArray":
{
"available": true,
"placeAfter": "string",
"placeBefore": "string",
"slotNumber": 1
}
}
{
"dateTime": "2017-05-25T09:20:58.846Z",
"timeSlotArray":
{
"available": true,
"placeAfter": "string",
"placeBefore": "string",
"slotNumber": 2
}
}
{
"dateTime": "2017-05-25T09:20:58.846Z",
"timeSlotArray":
{
"available": true,
"placeAfter": "string",
"placeBefore": "string",
"slotNumber": 3
}
}
{
"dateTime": "2017-05-25T09:20:58.846Z",
"timeSlotArray":
{
"available": true,
"placeAfter": "string",
"placeBefore": "string",
"slotNumber": 4
}
}
{
"dateTime": "2017-05-25T09:20:58.846Z",
"timeSlotArray":
{
"available": true,
"placeAfter": "string",
"placeBefore": "string",
"slotNumber": 5
}
}
but i'm failing to add each numbers to slotNumber
var startDate = new Date()
var endDate = new Date(startDate.getTime() + 7 * 24 * 60 * 60 * 1000);
var date = new Array()
for (var iDate = new Date(); iDate < endDate; iDate.setDate(iDate.getDate() + 1)) {
date.push(iDate)
}
var numbers = [0, 1, 2, 3, 4, 5]
addDateData = function(x) {
return {
date: x,
timeSlot: {}
}
}
addTimeSlot = function(x) {
_.each(numbers, function(y) {
_.extend(x.timeSlot, {
available: true,
placeAfter: "",
placeBefore: "",
slotNumber: y
})
})
}
var data = _.chain(date)
.map(addDateData)
.each(addTimeSlot)
.value()
console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
anyone can help me to understand what i'm doing wrong?
Upvotes: 0
Views: 73
Reputation: 5516
I believe if you simplify your addTimeSlot
function, you will get the result you are looking for.
addTimeSlot = function(x, index) {
_.extend(x.timeSlot, {
available: true,
placeAfter: "",
placeBefore: "",
slotNumber: index
})
}
If I understand correctly, it looks like your slotNumber
attributes are just index numbers. Since you are using the addTimeSlot
function inside of an each
method, you could just use the index numbers themselves.
Demo: https://jsfiddle.net/yffvw126/
Upvotes: 0
Reputation: 21881
I'm still not that "satisfied" with the expected result, but I will give it a shot.
This will add the days consecutively in the same array without any separation.
[
{ day: 1, timeSlot: 0 },
{ day: 1, timeSlot: 1 },
{ day: 2, timeSlot: 0 },
{ day: 2, timeSlot: 1 },
//...
]
var startDate = new Date(),
endDate = new Date(startDate.getTime() + 7 * 24 * 60 * 60 * 1000),
numbers = [0, 1, 2, 3, 4, 5],
date = [];
for (var iDate = new Date(); iDate < endDate; iDate.setDate(iDate.getDate() + 1)) {
numbers.forEach(function(n) {
date.push({
date: new Date(iDate),
timeSlot: {
available: true,
placeAfter: "string",
placeBefore: "string",
slotNumber: n
}
});
});
}
console.log(date);
Upvotes: 1