Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>No need to write your own...</strong></p> <p>The <a href="http://github.com/evanplaice/jquery-csv/" rel="noreferrer">jQuery-CSV</a> library has a function called <code>$.csv.toObjects(csv)</code> that does the mapping automatically.</p> <p><em>Note: The library is designed to handle any CSV data that is <a href="http://tools.ietf.org/html/rfc4180" rel="noreferrer">RFC 4180</a> compliant, including all of the nasty edge cases that most 'simple' solutions overlook.</em></p> <p>Like @Blazemonger already stated, first you need to add line breaks to make the data valid CSV.</p> <p><strong>Using the following dataset:</strong></p> <pre><code>heading1,heading2,heading3,heading4,heading5 value1_1,value2_1,value3_1,value4_1,value5_1 value1_2,value2_2,value3_2,value4_2,value5_2 </code></pre> <p><strong>Use the code:</strong></p> <pre><code>var data = $.csv.toObjects(csv): </code></pre> <p><strong>The output saved in 'data' will be:</strong></p> <pre><code>[ { heading1:"value1_1",heading2:"value2_1",heading3:"value3_1",heading4:"value4_1",heading5:"value5_1" } { heading1:"value1_2",heading2:"value2_2",heading3:"value3_2",heading4:"value4_2",heading5:"value5_2" } ] </code></pre> <p><em>Note: Technically, the way you wrote the key-value mapping is invalid JavaScript. The objects containing the key-value pairs should be wrapped in brackets.</em></p> <p>If you want to try it out for yourself, I suggest you take a look at the <a href="http://evanplaice.github.io/jquery-csv/examples/basic-usage.html" rel="noreferrer">Basic Usage Demonstration</a> under the 'toObjects()' tab.</p> <p><em>Disclaimer: I'm the original author of jQuery-CSV.</em></p> <p><strong>Update:</strong></p> <p>Edited to use the dataset that the op provided and included a link to the demo where the data can be tested for validity.</p> <p><strong>Update2:</strong></p> <p>Due to the shuttering of Google Code. <a href="http://github.com/evanplaice/jquery-csv/" rel="noreferrer">jquery-csv has moved to GitHub</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload