I have an object and i want to sort the items inside via forEach().
Each fifth element should be stored in a new array.
let content = []
let index = 0
Obj.forEach(function(item) {
if(item.includes('<h2>') && index !== 0) {
index++
}
content[index].push(item)
})
Unfortunately, i cannot push into content[index].push(item)
How i can create a new array, push new values and start a new array when my if is true?
>Solution :
You can use nullish coalescing assignment to create an array if it doesn’t already exist.
(content[index] ??= []).push(item);