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.

3
  • 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() Commented Jan 28, 2019 at 9: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. Commented Jan 28, 2019 at 9:27
  • You need to quite attribute names, no white space. Commented Jan 28, 2019 at 9:29

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

By the way I am not using JSON over there. I am adding array as per my requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.