Hi, I am trying to create a function for my array. It is going to take a parameter that i implement which is an array.
let dataStorage = [
"08:30", "10:30", "Project", "Nicosia",
"12:30", "14:30", "Selef", "Famagusta",
"16:30", "18:30", "Deneme", "Kyrenia",
"20:30", "22:30", "Sunum", "Laphitos"
];
Then,
let wordsPerLine = 4;
let result = [[],[],[],[]];
let len = dataStorage.length/4;
let n = dataStorage.length / 4;
for (let line = 0; line < n; line++) {
for (let i = 0; i < wordsPerLine; i++) {
const value = dataStorage[i + line * wordsPerLine]
result[line].push(value)
}
}
If my argument length is greater than 4, an undefined error appears.
Error:
denene.html:36 Uncaught TypeError: Cannot read property 'push' of undefined
I have to increment empty arrays by hand. But I want to create it dynamically according to my length.
My b
, For example, : If my array length is 16, automatically empty array is going to be created in results[] array.
Expected Output:
[Array(4),Array(4),Array(4),Array(4)]
->0:(4) ["08:30", "10:30", "Project", "Nicosia"],
->1:(4) ["08:30", "10:30", "Project", "Nicosia"],
->2:(4) ["08:30", "10:30", "Project", "Nicosia"],
->3:(4) ["08:30", "10:30", "Project", "Nicosia"],
What I have tried:
let dataStorage = [
"08:30", "10:30", "Project", "Nicosia",
"12:30", "14:30", "Selef", "Famagusta",
"16:30", "18:30", "Deneme", "Kyrenia",
"20:30", "22:30", "Sunum", "Laphitos",
"20:30", "22:30", "Sunum", "Laphitos",
"20:30", "22:30", "Sunum", "Laphitos"
];
console.log(dataStorage.length)
let wordsPerLine = 4;
let result = [[]];
let len = dataStorage.length/4;
let n = dataStorage.length / 4;
for (let line = 0; line < n; line++) {
for (let i = 0; i < wordsPerLine; i++) {
const value = dataStorage[i + line * wordsPerLine]
result[line].push(value)
}
}