I am scraping a website and have an array of numbers that I am looping over:
const arr = [1,2,3,1,2,4,5,6,7];
they correspond to win/lose/draw for different teams:
i.e.
1st item in array is won, second is draw, third is loss, fourth is won, fifth is draw, sixth is loss etc.
How can I loop through these so I have something like the following:
const teams = [{
won: 1,
draw: 2,
lost: 3
},{
won: 1,
draw: 2,
lost: 4
},{
won: 5,
draw: 6,
lost: 7
}];
I tried something like the following but it didn't work as I expected.
const arr = [1, 2, 3, 1, 2, 4, 5, 6, 7];
const newArr = [];
arr.forEach((item, index => {
if (index % 0 === 0) {
newArr.push({
won: item
});
} else if (index % 1 === 0) {
newArr.push({
draw: item
});
} else {
newArr.push({
lost: item
});
}
});
%does,index % 1means the remainder whenindexis divided by 1, which is always 0 (for non-negative integers).