I'm working with an array that looks like this:
public static readonly List: Array<any> = [
{ name: 'CCS', link: 'Dummy link1' },
{ name: 'CCR', link: 'Dummy link2' },
{ name: 'PM', link: 'Dummy link3' },
{ name: 'CM', link: 'Dummy link4' },
{ name: 'JM', link: 'Dummy link5' },
{ name: 'PSM', link: 'Dummy link6' }
];
My goal is to split this array into smaller arrays each containing 3 elements, like so:
ArrayFinal[{
{ name: 'CCS', link: 'Dummy link1' },
{ name: 'CCR', link: 'Dummy link2' },
{ name: 'PM', link: 'Dummy link3' }
},
{
{ name: 'CM', link: 'Dummy link4' },
{ name: 'JM', link: 'Dummy link5' },
{ name: 'PSM', link: 'Dummy link6' }
}]
I've attempted to accomplish this by checking for a modulus of 3 and then creating the new arrays accordingly, but I seem to be facing some issues.
Any guidance or assistance in solving this problem would be greatly appreciated.
UPDATE Here is what I have tried so far:
public func(): any {
for (let i = 1; i <= Array.List.length; i++) {
if (i % 3 === 0) {
this.ArrayFinal[i] = [Array.List[i], Array.List[i - 1],
Array.List[i - 2]];
}
}
However, it appears to be skipping the first element, and I am struggling to understand why. Any insight on this issue would be incredibly helpful.