I have a very simple question and am amazed I haven't figured it out yet. What am I missing? I get a php array through json encoding:
var curr_op = <?php echo json_encode($options) ?>;
The console.log(curr_op) reads:
[14:35:00.889] ({' ':" ", 'Tangential-S1':"Tangential-S1", 'Diagonal-Q1':"Diagonal-Q1", addnew:"Add New"})
and I would like to pass just the keys (or values) to html via:
$('#modal-body span2').html(JSON.stringify(curr_op));
which gives me an ugly thing:
{" ":" ","Tangential-S1":"Tangential-S1","Diagonal-Q1":"Diagonal-Q1","addnew":"Add New"}
in my webpage. How do I clean this up and only pass something like:
Tangential-S1, Diagonal-Q1
To the html (note the first and last elements should be removed too).
curr_opis an object. But the bigger issue is: Object properties are not ordered. You cannot talk about the "last" or "first" property of an object. Different browser might iterate over them in different order. If you only want specific keys I suggest you create an array on the server side only containing those keys.json_decodein your PHP script to serialize some data, in the JavaScript script you will end up having something likevar curr_op = {"foo": "bar"};which is an object literal (which creates an object). JSON looks very similar to that (because its syntax was inspired by JS' object literal syntax) but its independent from JS. JSON is a data-exchange format, like XML or CSV. It's just a way to describe data as text.