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:
- Data array from key 0 to 4 of the period must contain "
2010-10-10". - data array from key 5 to 10 period must contain "
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",
},
]
.map()which take a predicate as argument (basically a function), so you can make a filter in it (if e.period === ?), then make your changes there.