Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
       //Ic array size
       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)
           }

       }
Posted
Updated 14-Jan-21 4:23am

1 solution

Should be simple enough:
JavaScript
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"
];

let result = [];
const wordsPerLine = 4;

for (let line = 0; line < dataStorage.length; line += wordsPerLine) {
    let lineWords = [];
    for (let i = 0; i < wordsPerLine; i++) {
        const value = dataStorage[line + i];
        lineWords.push(value);
    }
    
    result.push(lineWords);
}
Demo[^]
 
Share this answer
 
Comments
slex1one-Musdy 14-Jan-21 11:45am    
Thank you so much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900