Skip to content

Instantly share code, notes, and snippets.

@joshbuchea
Created January 22, 2018 01:18
Show Gist options
  • Save joshbuchea/2abc45150db447431778e1adf48bc982 to your computer and use it in GitHub Desktop.
Save joshbuchea/2abc45150db447431778e1adf48bc982 to your computer and use it in GitHub Desktop.

Revisions

  1. @Frondor Frondor revised this gist Aug 15, 2017. No changes.
  2. @Frondor Frondor revised this gist Aug 15, 2017. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions suckData.js
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,8 @@
    * ~ but a personal snippet
    *
    * Sometimes when prototyping my designs, I like to get dummy data pretty fast.
    * Although it takes almost no time to use a third party service to create random/dummy data,
    * if I have a working example of a HTML table just like I want to dynamically render mine, I'll need
    * that same data but inside an object.
    * I use this snippet to extract that data from working (aka "real life") tables from other HTML tables I find on the net.
    * So I'll need that same data but inside an object.
    *
    * This script assumes by default the table has the following structure:
    * <table>
  3. @Frondor Frondor created this gist Aug 15, 2017.
    44 changes: 44 additions & 0 deletions suckData.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    /*
    * THIS IS NOT A PLUGIN
    * ~ but a personal snippet
    *
    * Sometimes when prototyping my designs, I like to get dummy data pretty fast.
    * Although it takes almost no time to use a third party service to create random/dummy data,
    * if I have a working example of a HTML table just like I want to dynamically render mine, I'll need
    * that same data but inside an object.
    *
    * This script assumes by default the table has the following structure:
    * <table>
    * <thead>
    * <tr><th>
    * <tbody>
    * <tr><td>
    */

    var o = { // default options
    selectors: {
    table: 'table',
    th: 'thead th', // relative to $table
    tr: 'tbody tr' // relative to $table
    }
    },
    $table,
    data = [],
    headings = [],
    log = console.log;

    $table = document.querySelector(o.selectors.table);

    // Get headings
    $table.querySelectorAll(o.selectors.th).forEach((th) => {
    headings[headings.length] = th.innerText;
    });

    $table.querySelectorAll(o.selectors.tr).forEach((tr, row) => {
    var obj = data[row] = {};
    tr.querySelectorAll('td').forEach((td, i) => {
    obj[headings[i]] = td.innerText;
    })
    });

    log(JSON.stringify(data));