Actually I have an array of objects to transform.
[
{ "val1":"2.0" },
{ "val2":"2.0" },
{ "val3":"2.0" },
{ "val1":"4.0" },
{ "val2":"2.0" },
{ "val3":"4.7" },
{ "val1":"4.0" },
{ "val2":"4.0" },
{ "val3":"4.0" }
]
I should take every three items and put them in a separate array with a parent item.
I would transform it to this format
[
[
"20190201",
[
{ "val1":"2.0" },
{ "val2":"2.0" },
{ "val3":"2.0" }
]
],
[
"20190202",
[
{ "val2":"2.0" },
{ "val3":"2.0" },
{ "val1":"4.0" }
]
]
]
Actually here's how I made
const returnData= fileDateName.map((_fileDate: string, _index: number) => {
return [_fileDate, [
filteredData[_index],
filteredData[_index + 1],
filteredData[_index + 2],
]];
});
My actual problem is that my output don't take the next three values but every time it shift one item then take the next values. I think it's because that _index
value doesn't increment as it should.
Index =0
filteredData[0],
filteredData[1],
filteredData[2],
index=1
filteredData[1],
filteredData[2],
filteredData[3],
...
but the iteration should make
Index =0
filteredData[0],
filteredData[1],
filteredData[2],
index=3
filteredData[3],
filteredData[4],
filteredData[5],
...
Edit
FileDateName= ["20190201","20190202","20190203"]
How could it set index value every iteration ?
20190201
and20190202
come from?fileDateName
it contains the length of input array /3 valuefileDateName
?