clone of HTML tables of data Need help to refactor this code to look like its done by a pro. Or just put some advice or links to useful stuff.
The idea is to clone a div with two empty tables for two separate arrays of data.
div is cloned depending on the longest array length between the two. Then append row of data to them, if rows reach max number the loop continues on another cloned table. loop continues even if the one row is finished appending
&lt;div id=&quot;d-page-0&quot;&gt;
    &lt;table id=&quot;e-tbl-0&quot;&gt;
        //content1
        //7 row max
    &lt;/table&gt;

    &lt;table id=&quot;w-tbl-0&quot;&gt;
        //content2
        //21 row max
    &lt;/table&gt;
&lt;/div&gt;

Javascript part
let d1 = @json($data1); //get data
let d2 = @json($data2); //get data

let ar1 = [];
let ar2 = [];
let s = 0;

for (let i = 7; i &lt; d1.length; i+=7) {
    ar1.push(d1.slice(i, i+7));           //slice data 1
}

for (let i = 21; i &lt; d2.length; i+=21) {
    ar2.push(d2.slice(i, i+21));          //slice data 2
}

ar1.length &gt; ar2.length ? s = ar1.length : s = ar2.length; //take longest array length

for (let v = 0; v &lt; s-1; v++) { //clone div using longest array length
     $(&quot;#d-page-0&quot;).clone().prop(&quot;id&quot;, &quot;d-page-&quot;+(v+1)).insertAfter(&quot;#d-page-&quot;+v); //clone div, edit id
     $(&quot;#d-page-&quot;+(v+1)).find(&quot;#e-tbl-0&quot;, &quot;table&quot;).prop(&quot;id&quot;, &quot;e-tbl-&quot;+(v+1)); //edit 1st tbl id
     $(&quot;#d-page-&quot;+(v+1)).find(&quot;#w-tbl-0&quot;, &quot;table&quot;).prop(&quot;id&quot;, &quot;w-tbl-&quot;+(v+1)); //edit 2nd tbl id
}

//append 1st data to 1st table
 for (let x = 0; x &lt; ar1.length; x++) {
      let k = ar1[x];
      for (let y = 0; y &lt; k.length; y++) {
           $('#e-tbl-'+x).append(
                `&lt;tr&gt;
                    &lt;td&gt;
                      ar1[x][y].content        
                    &lt;/td&gt;
                 &lt;/tr&gt;`
            );
      }
}

//append 2nd data to 2nd table
 for (let x = 0; x &lt; ar2.length; x++) {
      let k = ar2[x];
      for (let y = 0; y &lt; k.length; y++) {
           $('#w-tbl-'+x).append(
                `&lt;tr&gt;
                    &lt;td&gt;
                      ar1[x][y].content        
                    &lt;/td&gt;
                 &lt;/tr&gt;`
            );
      }
}

working sample
https://jsfiddle.net/eeneg/pgk3dhac/57/
I've refactored your code below to add some of the repetitive items into functions and tried to improve your variable names a little.
The new variable names and function names should clearly indicate their intended purpose/data.
A rule of thumb worth remembering is the concept of DRY (Do not Repeat Yourself).


const createArraywithJsonData = function(count, data) {
    let array = [];
    
    for (let i = count; i &lt; data.length; i += count) {
        array.push(data.slice(i, i + count));
    }
    
    return array;
}

const editTableId = function(selector, counter) {
    $(`#d-page-${(counter+1)}`).find(`#${selector}-0`, "table").prop("id", `${selector}-${(counter+1)}`);
}

const appendDataToTable = function(array, property, selector) {
    for (let x = 0; x &lt; array.length; x++) {
        let k = array[x];

        for (let y = 0; y &lt; k.length; y++) {
            $(`#${selector}-${x}`).append(`&lt;tr&gt;&lt;td&gt;${array[x][y][property]}&lt;/td&gt;&lt;/tr&gt;`);
        }
    }
}

let d1 = [
    {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }, {
        a: "1"
    }
];
let d2 = [
    {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }, {
        b: "2"
    }
];

let ar1 = createArraywithJsonData(7, d1);
let ar2 = createArraywithJsonData(21, d2);
let longestArrayLength = ar1.length &gt; ar2.length ? ar1.length : ar2.length;


for (let v = 0; v &lt; longestArrayLength - 1; v++) { //clone div using longest array length
    $("#d-page-0").clone().prop("id", `d-page-${(v + 1)}`).insertAfter(`#d-page-${v}`); //clone div, edit id
    editTableId('e-tbl', v);
    editTableId('w-tbl', v);
}

appendDataToTable(ar1, 'a', 'e-tbl');
appendDataToTable(ar2, 'b', 'w-tbl');
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
&lt;!DOCTYPE html&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang=""&gt;
  &lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;
  &lt;/head&gt;

  &lt;body bgcolor="#A0A0A0" vlink="blue" link="blue"&gt;
    &lt;div id="d-page-0"&gt;
        &lt;table id="e-tbl-0"&gt;
            &lt;tr&gt;&lt;th&gt;tbl1&lt;/th&gt;&lt;/tr&gt;
        &lt;/table&gt;

        &lt;table id="w-tbl-0"&gt;
            &lt;tr&gt;&lt;th&gt;tbl2&lt;/th&gt;&lt;/tr&gt;
        &lt;/table&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;