Set First Value as Key in Javascript Array

  • A+

Creating an array based off selected DataTables Rows

$('#savenlp').click(recordjourney);  function recordjourney() { var data = table.rows(['.selected']).data().toArray(); console.log( (data) ); console.log( JSON.stringify(data) ); } 

data returns

0 : (8) ["Which", "TitleCase", "QuestionWord", "", "", "", "", ""] 

JSON.stringify(data) returns


This information is dynamically generated, so I am just looking to take the first value (in this case baseball) and turn it into something like

  "baseball": [     "Noun",     "Singular"   ] 

I can return the first value (the key I want using)


I am much more adept in PHP but I am learning javascript/jquery more and more.

It is my understanding javascript does not have associative arrays, so I am a bit confused as to how to generate this.

const data = [   ["baseball","Noun","Singular","","","","",""],   ["baseballs","Noun","","Plural","","","","",] ]; const mappedData = data.reduce((acc, row) => { acc[row.shift()] = row.filter(d => d !== ''); return acc; }, {}); console.log(mappedData);


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: