Reputation: 89
I have an array with different amounts of data, I want to replace the period contents of the array with the data I want for example:
2010-10-10
".2010-11-11
"Condition: I need an array of data from 0-4, 5-9, 10-14 (multiples of 5) The following data I have:
[
{
"id": "1",
"name": "SUNARTA",
"period":"",
},
{
"id": "2",
"name": "AAN",
"period":"",
},
{
"id": "3",
"name": "MIKO",
"period":"",
},
{
"id": "4",
"name": "NIKA",
"period":"",
},
{
"id": "5",
"name": "LALA",
"period":"",
},
{
"id": "6",
"name": "MMAAN",
"period":"",
},
{
"id": "7",
"name": "NINA",
"period":"",
}
]
This is my code:
for (var i = 0; i < element.length; i++) {
var datePeriod = new Date(req.body.start);
var periode = datePeriod;
if (i % 5 == 0) {
datePeriod.setDate(datePeriod.getDate() + 1);
element[i].periode = periode;
} else {
element[i].periode = periode;
}
}
I want data should be:
[
{
"id": "1",
"name": "SUNARTA",
"period":"2010-10-10",
},
{
"id": "2",
"name": "AAN",
"period":"2010-10-10",
},
{
"id": "3",
"name": "MIKO",
"period":"2010-10-10",
},
{
"id": "4",
"name": "NIKA",
"period":"2010-10-10",
},
{
"id": "5",
"name": "LALA",
"period":"2010-10-10",
},
{
"id": "6",
"name": "MMAAN",
"period":"2011-11-11",
},
{
"id": "7",
"name": "NINA",
"period":"2011-11-11",
},
]
Upvotes: 1
Views: 274
Reputation: 39250
If you want to increase the date with one day for every 5 items then you can do the following:
A function that takes a start date and a number (index of the array) and add days to the start date depending on the number:
function calcDate(startDate, i) {
return formatDate(
new Date(
new Date(startDate).setDate(
//add a day every 5 days
startDate.getDate() + Math.floor(i / 5),
),
),
);
}
Now create a variable holding the start date: var date = new Date();
Use Array.prototype.map to map the array you have to create a new array where period is set with the result of the calculate date function passing in the start date and the index of the current item:
var result = data.map(function(d, i) {
return Object.assign({}, d, {
period: calcDate(date, i),
});
});
Complete code:
var data = [{"id":"1","name":"SUNARTA","period":""},{"id":"2","name":"AAN","period":""},{"id":"3","name":"MIKO","period":""},{"id":"4","name":"NIKA","period":""},{"id":"5","name":"LALA","period":""},{"id":"6","name":"MMAAN","period":""},{"id":"7","name":"NINA","period":""}];
function pad(num) {
return ('000' + num).slice(-2);
}
function formatDate(date) {
return [
date.getFullYear(),
pad(date.getMonth() + 1),
pad(date.getDate()),
].join('-');
}
function calcDate(startDate, i) {
return formatDate(
new Date(
new Date(startDate).setDate(
//add a day every 5 days
startDate.getDate() + Math.floor(i / 5),
),
),
);
}
var date = new Date();
var result = data.map(function(d, i) {
return Object.assign({}, d, {
period: calcDate(date, i),
});
});
console.log(result);
Upvotes: 1
Reputation: 147363
You are very close to a solution. Your issue is that you're reinitialising datePeriod on each loop. You only need to do that once, then increment it on every 5th element. So move the initialisation outside the for loop (and tidy a few other things):
var element = [{"id": "1","name": "SUNARTA","period": ""},
{"id": "2","name": "AAN","period": ""},
{"id": "3","name": "MIKO","period": ""},
{"id": "4","name": "NIKA","period": ""},
{"id": "5","name": "LALA","period": ""},
{"id": "6","name": "MMAAN","period": ""},
{"id": "7","name": "NINA","period": ""}];
// Helper to format the date
function formatDate(d) {
return `${d.getFullYear()}-${('0'+d.getMonth()).slice(-2)}-${('0'+d.getDate()).slice(-2)}`;
}
// Initialise datePeriod outside the loop
var datePeriod = new Date();
for (var i = 0; i < element.length; i++) {
// This variable is unnecessary
//var periode = datePeriod;
// Increment datePeriod every 5th element
// The else loop isn't necessary
if (i && i % 5 == 0) {
datePeriod.setDate(datePeriod.getDate() + 1);
}
element[i].period = formatDate(datePeriod);
}
console.log(element)
This creates just one Date object, so it will be reasonably efficient.
Upvotes: 1
Reputation: 22524
You can use array#map
and iterate through each object based on the index increment date after every 5 iteration and update the period
property.
const data = [ { "id": "1", "name": "SUNARTA", "period":"", }, { "id": "2", "name": "AAN", "period":"", }, { "id": "3", "name": "MIKO", "period":"", }, { "id": "4", "name": "NIKA", "period":"", }, { "id": "5", "name": "LALA", "period":"", }, { "id": "6", "name": "MMAAN","period":"", }, { "id": "7", "name": "NINA", "period":"", }],
size = 5,
startDate = '2010-10-10',
result = data.map((o,i) => {
let index = Math.floor(i/size);
let period = i == 0 ? new Date(startDate) :
(date => (new Date(date.setDate(date.getDate() + index))))(new Date(startDate));
period = period.toISOString().substring(0,10);
return {...o, period};
},[]);
console.log(result);
Upvotes: 1