I am using Jquery to get all products name from page and than put it on array. I am using this code
<script type="text/javascript">
jQuery(document).ready(function($) {
var products = $(".product-name").map(function() {
return { name: $(this).text() }
}) .get();
console.log(JSON.stringify(products));
});
</script>
This give me output in this format
[{"name":"Sample Product Name"},{"name":"Sample Product Name 2"}]
What I am trying to achieve is to have one space in between these two objects after "," so the output will look something like this
[{"name":"Sample Product Name"}, {"name":"Sample Product Name 2"}]
Any advise? I am struggling from hours and no success.
Here is the jsfiddle http://jsfiddle.net/2MeMY/1/
This may not be what you want, but if you just want it to look better, I would recommend:
console.log(JSON.stringify(products, null, 2));
which would give you
[
{
"name": "Sample Product Name"
},
{
"name": "Sample Product Name 2"
}
]
In the console. If you really just want a space before commas, you could do:
console.log(JSON.stringify(products).split('},{').join('}, {'));