I am new to Javascript, so my apologies if this is a dumb question. I've read a number of threads on here and other sites looking for an answer, and yet to find a solution. I have borrowed part of the code below from another post here.
I use jquery to count some things on my web page so I can display the numbers. I also now want to add those numbers in a bit of JSON FAQ schema for Google to use. How can I get the variables to output properly in the below example? I can get them to output to the html using .text but now need that value in the schema as well.
Thank you
var dealcount = jQuery('article.deal').length;
var codecount = jQuery('article.code').length;
var printcount = jQuery('article.print').length;
var totalcount = jQuery('article.card').length;
var d = new Date();
d.setDate(d.getDate() - 1);
var n = d.toDateString().split(' ').slice(1).join(' ');
var schema = {
"@context": "http://schema.org",
"@type": "FAQPage",
"mainEntity": [{
"@type": "Question",
"name": "How many things are there?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Currently, there are (DEALCOUNT) different offers including (CODECOUNT) codes, and (PRINTCOUNT) printables as of (n)"
}
}]
}
var script = document.createElement('script');
script.type = "application/ld+json";
script.text = JSON.stringify(schema);
document.querySelector('body').appendChild(script);