How can I change the length of each table to be equal to the longest one provided that the longest one has a...











up vote
2
down vote

favorite












Let's assume that I have a 2D array in typescript. Exactly in the source code it looks like this:



    tsvData: any;
this.tsvData = this.UploaderService.tsvData.split("n").map(function(row){return row.split("t");});


I would like to change the length of each table to be equal to the longest one provided that the longest one has a length greater than or equal to 5. If the length of the longest table is less than 5 I would like to change the length of each table to 5. I would like to fill the missing places with an empty string. I add two examples for understanding what my goal is. In addition, I would like to delete all arrays with empty strings.



Example 1



In this example the longest array is equals to 4 so I change the length of each table to 5.



Input:



[["text1", "text2", "text3", "text4"], 
["text5", "text6"],
["text7"],
["text8", "text9", "text10"],
[""]]


Output:



[["text1", "text2", "text3", "text4", ""], 
["text5", "text6", "", "", ""],
["text7", "", "", "", ""],
["text8", "text9", "text10", "", ""]]


Example 2



In this example the longest array is equals to 6 so I change the length of each table to 6.



Input:



[["text1", "text2", "text3", "text4"], 
["text5", "text6"],
["text7"],
["text8", "text9", "text10", "text11", "text12", "text13"],
[""]]


Output:



[["text1", "text2", "text3", "text4", "", ""], 
["text5", "text6", "", "", "", ""],
["text7", "", "", "", "", ""],
["text8", "text9", "text10", "text11", "text12", "text13"]]









share|improve this question




























    up vote
    2
    down vote

    favorite












    Let's assume that I have a 2D array in typescript. Exactly in the source code it looks like this:



        tsvData: any;
    this.tsvData = this.UploaderService.tsvData.split("n").map(function(row){return row.split("t");});


    I would like to change the length of each table to be equal to the longest one provided that the longest one has a length greater than or equal to 5. If the length of the longest table is less than 5 I would like to change the length of each table to 5. I would like to fill the missing places with an empty string. I add two examples for understanding what my goal is. In addition, I would like to delete all arrays with empty strings.



    Example 1



    In this example the longest array is equals to 4 so I change the length of each table to 5.



    Input:



    [["text1", "text2", "text3", "text4"], 
    ["text5", "text6"],
    ["text7"],
    ["text8", "text9", "text10"],
    [""]]


    Output:



    [["text1", "text2", "text3", "text4", ""], 
    ["text5", "text6", "", "", ""],
    ["text7", "", "", "", ""],
    ["text8", "text9", "text10", "", ""]]


    Example 2



    In this example the longest array is equals to 6 so I change the length of each table to 6.



    Input:



    [["text1", "text2", "text3", "text4"], 
    ["text5", "text6"],
    ["text7"],
    ["text8", "text9", "text10", "text11", "text12", "text13"],
    [""]]


    Output:



    [["text1", "text2", "text3", "text4", "", ""], 
    ["text5", "text6", "", "", "", ""],
    ["text7", "", "", "", "", ""],
    ["text8", "text9", "text10", "text11", "text12", "text13"]]









    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Let's assume that I have a 2D array in typescript. Exactly in the source code it looks like this:



          tsvData: any;
      this.tsvData = this.UploaderService.tsvData.split("n").map(function(row){return row.split("t");});


      I would like to change the length of each table to be equal to the longest one provided that the longest one has a length greater than or equal to 5. If the length of the longest table is less than 5 I would like to change the length of each table to 5. I would like to fill the missing places with an empty string. I add two examples for understanding what my goal is. In addition, I would like to delete all arrays with empty strings.



      Example 1



      In this example the longest array is equals to 4 so I change the length of each table to 5.



      Input:



      [["text1", "text2", "text3", "text4"], 
      ["text5", "text6"],
      ["text7"],
      ["text8", "text9", "text10"],
      [""]]


      Output:



      [["text1", "text2", "text3", "text4", ""], 
      ["text5", "text6", "", "", ""],
      ["text7", "", "", "", ""],
      ["text8", "text9", "text10", "", ""]]


      Example 2



      In this example the longest array is equals to 6 so I change the length of each table to 6.



      Input:



      [["text1", "text2", "text3", "text4"], 
      ["text5", "text6"],
      ["text7"],
      ["text8", "text9", "text10", "text11", "text12", "text13"],
      [""]]


      Output:



      [["text1", "text2", "text3", "text4", "", ""], 
      ["text5", "text6", "", "", "", ""],
      ["text7", "", "", "", "", ""],
      ["text8", "text9", "text10", "text11", "text12", "text13"]]









      share|improve this question















      Let's assume that I have a 2D array in typescript. Exactly in the source code it looks like this:



          tsvData: any;
      this.tsvData = this.UploaderService.tsvData.split("n").map(function(row){return row.split("t");});


      I would like to change the length of each table to be equal to the longest one provided that the longest one has a length greater than or equal to 5. If the length of the longest table is less than 5 I would like to change the length of each table to 5. I would like to fill the missing places with an empty string. I add two examples for understanding what my goal is. In addition, I would like to delete all arrays with empty strings.



      Example 1



      In this example the longest array is equals to 4 so I change the length of each table to 5.



      Input:



      [["text1", "text2", "text3", "text4"], 
      ["text5", "text6"],
      ["text7"],
      ["text8", "text9", "text10"],
      [""]]


      Output:



      [["text1", "text2", "text3", "text4", ""], 
      ["text5", "text6", "", "", ""],
      ["text7", "", "", "", ""],
      ["text8", "text9", "text10", "", ""]]


      Example 2



      In this example the longest array is equals to 6 so I change the length of each table to 6.



      Input:



      [["text1", "text2", "text3", "text4"], 
      ["text5", "text6"],
      ["text7"],
      ["text8", "text9", "text10", "text11", "text12", "text13"],
      [""]]


      Output:



      [["text1", "text2", "text3", "text4", "", ""], 
      ["text5", "text6", "", "", "", ""],
      ["text7", "", "", "", "", ""],
      ["text8", "text9", "text10", "text11", "text12", "text13"]]






      arrays angular typescript multidimensional-array angular6






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday

























      asked yesterday









      thedbogh

      708




      708
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Let's break it down into steps:




          1. Find the max length in the arrays of the 2D array.

          2. Check if it's >= 5. Based on this, create a maxLength.

          3. Then loop through the 2D array, create the rest array and fill it with "".

          4. Merge the rest array with each item of the 2D array.


          Try this:






          var initialTime = performance.now();

          var array1 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]
          ];

          var array2 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10", "text11", "text12", "text13"],
          [""]
          ];

          function fillRestItems(arrays) {
          var maxArrayLength = Math
          .max(...arrays.map(array => array.length));

          var maxLength = maxArrayLength >= 5 ? maxArrayLength : 5;

          return arrays
          .map(arr => {
          if(!(arr.length === 1 && arr[0] === "")) {
          var addedArray = new Array(maxLength - arr.length).fill("");
          return [...arr, ...addedArray];
          }
          })
          .filter(array => array !== undefined);
          }

          console.log('Filled Array 1:', fillRestItems(array1));
          console.log('Filled Array 2: ', fillRestItems(array2));

          var finalTime = performance.now();
          console.log(`This took: ${(finalTime - initialTime)} ms`);








          share|improve this answer























          • This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
            – thedbogh
            yesterday










          • Thank you, both yours and the Sid's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
            – thedbogh
            yesterday










          • Yes, I updated the question. Thank you for your help.
            – thedbogh
            yesterday










          • I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. Your solution works properly unfortunately Sid's is not. I accept your solution.
            – thedbogh
            yesterday


















          up vote
          1
          down vote













          First step would be to find the sub-array of maximum size.



          let arr = [["text1", "text2", "text3", "text4"], 
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]];

          arr = arr.filter(e => e.every(x => x !== ""))

          const maxLength = Math.max(...arr.map(a => a.length));


          The next step is to fill the array with blank values.



          arr.forEach(e => {
          while(e.length < maxLength){
          e.push("");
          }
          });

          console.log(arr);


          I am sure there are better ways of doing it as the time complexity of the algorithm is O(n2)






          share|improve this answer























          • This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
            – thedbogh
            yesterday












          • Fixed the code to filter unwanted items.
            – Sid
            yesterday










          • Thank you, both yours and the SiddAjmera's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
            – thedbogh
            yesterday










          • Go with whoever answered first . ;)
            – Sid
            yesterday










          • I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. In your case it doesn't work. Performance in both cases is similar so in accordance to your suggestion I accept the solution of SiddAjmera.
            – thedbogh
            yesterday











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237873%2fhow-can-i-change-the-length-of-each-table-to-be-equal-to-the-longest-one-provide%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Let's break it down into steps:




          1. Find the max length in the arrays of the 2D array.

          2. Check if it's >= 5. Based on this, create a maxLength.

          3. Then loop through the 2D array, create the rest array and fill it with "".

          4. Merge the rest array with each item of the 2D array.


          Try this:






          var initialTime = performance.now();

          var array1 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]
          ];

          var array2 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10", "text11", "text12", "text13"],
          [""]
          ];

          function fillRestItems(arrays) {
          var maxArrayLength = Math
          .max(...arrays.map(array => array.length));

          var maxLength = maxArrayLength >= 5 ? maxArrayLength : 5;

          return arrays
          .map(arr => {
          if(!(arr.length === 1 && arr[0] === "")) {
          var addedArray = new Array(maxLength - arr.length).fill("");
          return [...arr, ...addedArray];
          }
          })
          .filter(array => array !== undefined);
          }

          console.log('Filled Array 1:', fillRestItems(array1));
          console.log('Filled Array 2: ', fillRestItems(array2));

          var finalTime = performance.now();
          console.log(`This took: ${(finalTime - initialTime)} ms`);








          share|improve this answer























          • This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
            – thedbogh
            yesterday










          • Thank you, both yours and the Sid's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
            – thedbogh
            yesterday










          • Yes, I updated the question. Thank you for your help.
            – thedbogh
            yesterday










          • I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. Your solution works properly unfortunately Sid's is not. I accept your solution.
            – thedbogh
            yesterday















          up vote
          1
          down vote



          accepted










          Let's break it down into steps:




          1. Find the max length in the arrays of the 2D array.

          2. Check if it's >= 5. Based on this, create a maxLength.

          3. Then loop through the 2D array, create the rest array and fill it with "".

          4. Merge the rest array with each item of the 2D array.


          Try this:






          var initialTime = performance.now();

          var array1 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]
          ];

          var array2 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10", "text11", "text12", "text13"],
          [""]
          ];

          function fillRestItems(arrays) {
          var maxArrayLength = Math
          .max(...arrays.map(array => array.length));

          var maxLength = maxArrayLength >= 5 ? maxArrayLength : 5;

          return arrays
          .map(arr => {
          if(!(arr.length === 1 && arr[0] === "")) {
          var addedArray = new Array(maxLength - arr.length).fill("");
          return [...arr, ...addedArray];
          }
          })
          .filter(array => array !== undefined);
          }

          console.log('Filled Array 1:', fillRestItems(array1));
          console.log('Filled Array 2: ', fillRestItems(array2));

          var finalTime = performance.now();
          console.log(`This took: ${(finalTime - initialTime)} ms`);








          share|improve this answer























          • This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
            – thedbogh
            yesterday










          • Thank you, both yours and the Sid's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
            – thedbogh
            yesterday










          • Yes, I updated the question. Thank you for your help.
            – thedbogh
            yesterday










          • I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. Your solution works properly unfortunately Sid's is not. I accept your solution.
            – thedbogh
            yesterday













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Let's break it down into steps:




          1. Find the max length in the arrays of the 2D array.

          2. Check if it's >= 5. Based on this, create a maxLength.

          3. Then loop through the 2D array, create the rest array and fill it with "".

          4. Merge the rest array with each item of the 2D array.


          Try this:






          var initialTime = performance.now();

          var array1 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]
          ];

          var array2 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10", "text11", "text12", "text13"],
          [""]
          ];

          function fillRestItems(arrays) {
          var maxArrayLength = Math
          .max(...arrays.map(array => array.length));

          var maxLength = maxArrayLength >= 5 ? maxArrayLength : 5;

          return arrays
          .map(arr => {
          if(!(arr.length === 1 && arr[0] === "")) {
          var addedArray = new Array(maxLength - arr.length).fill("");
          return [...arr, ...addedArray];
          }
          })
          .filter(array => array !== undefined);
          }

          console.log('Filled Array 1:', fillRestItems(array1));
          console.log('Filled Array 2: ', fillRestItems(array2));

          var finalTime = performance.now();
          console.log(`This took: ${(finalTime - initialTime)} ms`);








          share|improve this answer














          Let's break it down into steps:




          1. Find the max length in the arrays of the 2D array.

          2. Check if it's >= 5. Based on this, create a maxLength.

          3. Then loop through the 2D array, create the rest array and fill it with "".

          4. Merge the rest array with each item of the 2D array.


          Try this:






          var initialTime = performance.now();

          var array1 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]
          ];

          var array2 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10", "text11", "text12", "text13"],
          [""]
          ];

          function fillRestItems(arrays) {
          var maxArrayLength = Math
          .max(...arrays.map(array => array.length));

          var maxLength = maxArrayLength >= 5 ? maxArrayLength : 5;

          return arrays
          .map(arr => {
          if(!(arr.length === 1 && arr[0] === "")) {
          var addedArray = new Array(maxLength - arr.length).fill("");
          return [...arr, ...addedArray];
          }
          })
          .filter(array => array !== undefined);
          }

          console.log('Filled Array 1:', fillRestItems(array1));
          console.log('Filled Array 2: ', fillRestItems(array2));

          var finalTime = performance.now();
          console.log(`This took: ${(finalTime - initialTime)} ms`);








          var initialTime = performance.now();

          var array1 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]
          ];

          var array2 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10", "text11", "text12", "text13"],
          [""]
          ];

          function fillRestItems(arrays) {
          var maxArrayLength = Math
          .max(...arrays.map(array => array.length));

          var maxLength = maxArrayLength >= 5 ? maxArrayLength : 5;

          return arrays
          .map(arr => {
          if(!(arr.length === 1 && arr[0] === "")) {
          var addedArray = new Array(maxLength - arr.length).fill("");
          return [...arr, ...addedArray];
          }
          })
          .filter(array => array !== undefined);
          }

          console.log('Filled Array 1:', fillRestItems(array1));
          console.log('Filled Array 2: ', fillRestItems(array2));

          var finalTime = performance.now();
          console.log(`This took: ${(finalTime - initialTime)} ms`);





          var initialTime = performance.now();

          var array1 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]
          ];

          var array2 = [
          ["text1", "text2", "text3", "text4"],
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10", "text11", "text12", "text13"],
          [""]
          ];

          function fillRestItems(arrays) {
          var maxArrayLength = Math
          .max(...arrays.map(array => array.length));

          var maxLength = maxArrayLength >= 5 ? maxArrayLength : 5;

          return arrays
          .map(arr => {
          if(!(arr.length === 1 && arr[0] === "")) {
          var addedArray = new Array(maxLength - arr.length).fill("");
          return [...arr, ...addedArray];
          }
          })
          .filter(array => array !== undefined);
          }

          console.log('Filled Array 1:', fillRestItems(array1));
          console.log('Filled Array 2: ', fillRestItems(array2));

          var finalTime = performance.now();
          console.log(`This took: ${(finalTime - initialTime)} ms`);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          SiddAjmera

          8,99121037




          8,99121037












          • This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
            – thedbogh
            yesterday










          • Thank you, both yours and the Sid's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
            – thedbogh
            yesterday










          • Yes, I updated the question. Thank you for your help.
            – thedbogh
            yesterday










          • I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. Your solution works properly unfortunately Sid's is not. I accept your solution.
            – thedbogh
            yesterday


















          • This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
            – thedbogh
            yesterday










          • Thank you, both yours and the Sid's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
            – thedbogh
            yesterday










          • Yes, I updated the question. Thank you for your help.
            – thedbogh
            yesterday










          • I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. Your solution works properly unfortunately Sid's is not. I accept your solution.
            – thedbogh
            yesterday
















          This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
          – thedbogh
          yesterday




          This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
          – thedbogh
          yesterday












          Thank you, both yours and the Sid's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
          – thedbogh
          yesterday




          Thank you, both yours and the Sid's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
          – thedbogh
          yesterday












          Yes, I updated the question. Thank you for your help.
          – thedbogh
          yesterday




          Yes, I updated the question. Thank you for your help.
          – thedbogh
          yesterday












          I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. Your solution works properly unfortunately Sid's is not. I accept your solution.
          – thedbogh
          yesterday




          I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. Your solution works properly unfortunately Sid's is not. I accept your solution.
          – thedbogh
          yesterday












          up vote
          1
          down vote













          First step would be to find the sub-array of maximum size.



          let arr = [["text1", "text2", "text3", "text4"], 
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]];

          arr = arr.filter(e => e.every(x => x !== ""))

          const maxLength = Math.max(...arr.map(a => a.length));


          The next step is to fill the array with blank values.



          arr.forEach(e => {
          while(e.length < maxLength){
          e.push("");
          }
          });

          console.log(arr);


          I am sure there are better ways of doing it as the time complexity of the algorithm is O(n2)






          share|improve this answer























          • This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
            – thedbogh
            yesterday












          • Fixed the code to filter unwanted items.
            – Sid
            yesterday










          • Thank you, both yours and the SiddAjmera's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
            – thedbogh
            yesterday










          • Go with whoever answered first . ;)
            – Sid
            yesterday










          • I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. In your case it doesn't work. Performance in both cases is similar so in accordance to your suggestion I accept the solution of SiddAjmera.
            – thedbogh
            yesterday















          up vote
          1
          down vote













          First step would be to find the sub-array of maximum size.



          let arr = [["text1", "text2", "text3", "text4"], 
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]];

          arr = arr.filter(e => e.every(x => x !== ""))

          const maxLength = Math.max(...arr.map(a => a.length));


          The next step is to fill the array with blank values.



          arr.forEach(e => {
          while(e.length < maxLength){
          e.push("");
          }
          });

          console.log(arr);


          I am sure there are better ways of doing it as the time complexity of the algorithm is O(n2)






          share|improve this answer























          • This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
            – thedbogh
            yesterday












          • Fixed the code to filter unwanted items.
            – Sid
            yesterday










          • Thank you, both yours and the SiddAjmera's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
            – thedbogh
            yesterday










          • Go with whoever answered first . ;)
            – Sid
            yesterday










          • I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. In your case it doesn't work. Performance in both cases is similar so in accordance to your suggestion I accept the solution of SiddAjmera.
            – thedbogh
            yesterday













          up vote
          1
          down vote










          up vote
          1
          down vote









          First step would be to find the sub-array of maximum size.



          let arr = [["text1", "text2", "text3", "text4"], 
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]];

          arr = arr.filter(e => e.every(x => x !== ""))

          const maxLength = Math.max(...arr.map(a => a.length));


          The next step is to fill the array with blank values.



          arr.forEach(e => {
          while(e.length < maxLength){
          e.push("");
          }
          });

          console.log(arr);


          I am sure there are better ways of doing it as the time complexity of the algorithm is O(n2)






          share|improve this answer














          First step would be to find the sub-array of maximum size.



          let arr = [["text1", "text2", "text3", "text4"], 
          ["text5", "text6"],
          ["text7"],
          ["text8", "text9", "text10"],
          [""]];

          arr = arr.filter(e => e.every(x => x !== ""))

          const maxLength = Math.max(...arr.map(a => a.length));


          The next step is to fill the array with blank values.



          arr.forEach(e => {
          while(e.length < maxLength){
          e.push("");
          }
          });

          console.log(arr);


          I am sure there are better ways of doing it as the time complexity of the algorithm is O(n2)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Sid

          2,863103580




          2,863103580












          • This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
            – thedbogh
            yesterday












          • Fixed the code to filter unwanted items.
            – Sid
            yesterday










          • Thank you, both yours and the SiddAjmera's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
            – thedbogh
            yesterday










          • Go with whoever answered first . ;)
            – Sid
            yesterday










          • I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. In your case it doesn't work. Performance in both cases is similar so in accordance to your suggestion I accept the solution of SiddAjmera.
            – thedbogh
            yesterday


















          • This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
            – thedbogh
            yesterday












          • Fixed the code to filter unwanted items.
            – Sid
            yesterday










          • Thank you, both yours and the SiddAjmera's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
            – thedbogh
            yesterday










          • Go with whoever answered first . ;)
            – Sid
            yesterday










          • I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. In your case it doesn't work. Performance in both cases is similar so in accordance to your suggestion I accept the solution of SiddAjmera.
            – thedbogh
            yesterday
















          This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
          – thedbogh
          yesterday






          This is almost what I wanted to achieve, however in the case of [""] it returns ["", "", "", "", ""] but it should remove this array.
          – thedbogh
          yesterday














          Fixed the code to filter unwanted items.
          – Sid
          yesterday




          Fixed the code to filter unwanted items.
          – Sid
          yesterday












          Thank you, both yours and the SiddAjmera's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
          – thedbogh
          yesterday




          Thank you, both yours and the SiddAjmera's solution work perfectly. Of course, I voted for both, but I can only accept one. I must choose in that case a solution with better performance. Which one do you think is better?
          – thedbogh
          yesterday












          Go with whoever answered first . ;)
          – Sid
          yesterday




          Go with whoever answered first . ;)
          – Sid
          yesterday












          I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. In your case it doesn't work. Performance in both cases is similar so in accordance to your suggestion I accept the solution of SiddAjmera.
          – thedbogh
          yesterday




          I made a mistake. I didn't check a case when the longest array in the table is shorter than 5. In your case it doesn't work. Performance in both cases is similar so in accordance to your suggestion I accept the solution of SiddAjmera.
          – thedbogh
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237873%2fhow-can-i-change-the-length-of-each-table-to-be-equal-to-the-longest-one-provide%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation