0

I have array of object in data attribute and I want to get that value as array of object in js. I confused how to parse string to array in js. Here is my data attribute:

var values = [{title:"My Office",loc:{lat:27.7081018,lng:85.3342199}},{title:"My Hostel",loc:{lat:27.7072867,lng:85.3253844}},{title:"Sudhir House",loc:{lat:27.6802258,lng:85.3805697}},{title:"Indra Chowk",loc:{lat:27.7057217,lng:85.3084168}},{title:"Jamal",loc:{lat:27.7017848,lng:85.3127387}},{title:"Patan",loc:{lat:27.5978047,lng:85.355257}},{title:"Baktapur",loc:{lat:27.6773968,lng:85.406957}},{title:"Dhulikhel",loc:{lat:27.6241873,lng:85.5410204}},{title:"Nagarjun",loc:{lat:27.7249402,lng:85.3591267}},{title:"Chitlang",loc:{lat:27.6478865,lng:85.1335696}},{title:"Pilot baba ji",loc:{lat:27.6406024,lng:85.4202461}}];

// Result I want to get
console.log(values)

// Return string
console.log($('div').attr("data-values"));

// Return single array
console.log(new Array($('div').attr("data-values")));

// Return error
console.log(JSON.parse($('div').attr("data-values")));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-values='[{title:"My Office",loc:{lat:27.7081018,lng:85.3342199}},{title:"My Hostel",loc:{lat:27.7072867,lng:85.3253844}},{title:"Sudhir House",loc:{lat:27.6802258,lng:85.3805697}},{title:"Indra Chowk",loc:{lat:27.7057217,lng:85.3084168}},{title:"Jamal",loc:{lat:27.7017848,lng:85.3127387}},{title:"Patan",loc:{lat:27.5978047,lng:85.355257}},{title:"Baktapur",loc:{lat:27.6773968,lng:85.406957}},{title:"Dhulikhel",loc:{lat:27.6241873,lng:85.5410204}},{title:"Nagarjun",loc:{lat:27.7249402,lng:85.3591267}},{title:"Chitlang",loc:{lat:27.6478865,lng:85.1335696}},{title:"Pilot baba ji",loc:{lat:27.6406024,lng:85.4202461}}]'>
</div>

I the above code I have stored the array to data-values and when getting the value of data-values I got string. How can I parse that string back to array of an objects.

Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
  • 1
    This would be much easier if the string you are trying to parse was valid JSON. Since it's not, you could either change the string or *carefully* use `eval()` – Mark Jan 28 '19 at 09:22
  • 1
    Your json isn't valid. Fix that then `JSON.parse` will work. In fact if I remember correctly `.data()` will just give you an object without having to parse it at all. – Liam Jan 28 '19 at 09:27
  • You need to quite attribute names, no white space. – marekful Jan 28 '19 at 09:29

1 Answers1

1

It is because of invalid JSON format, the keys should be enclosed with double quotes.

[{"title":"My Office","loc":{"lat":27.7081018,"lng":85.3342199}}]

Note: For temporary workaround eval() works fine

User863
  • 19,346
  • 2
  • 17
  • 41
  • By the way I am not using JSON over there. I am adding array as per my requirement. – Albert Einstein Jan 28 '19 at 10:15
  • 1
    [Don't use `eval`, pretty much ever](https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea) – Liam Jan 28 '19 at 10:25