gpt4 book ai didi

javascript - 如何在 json 模式输出中使用 Javascript 变量

转载 作者:行者123 更新时间:2023-12-02 22:21:33 24 4
gpt4 key购买 nike

我是 Javascript 新手,所以如果这是一个愚蠢的问题,我很抱歉。我在这里和其他网站上阅读了许多帖子来寻找答案,但尚未找到解决方案。我从这里的另一篇文章中借用了下面的部分代码。

我使用 jquery 来计算网页上的一些内容,以便我可以显示数字。我现在还想将这些数字添加到一些 JSON FAQ 架构中以供 Google 使用。在下面的例子中如何让变量正确输出?我可以使用 .text 让它们输出到 html,但现在架构中也需要该值。

谢谢

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);

最佳答案

您可以使用template strings :

      "text": `Currently, there are ${dealcount} different offers including ${codecount} codes, and ${printcount} printables as of ${n}`

关于javascript - 如何在 json 模式输出中使用 Javascript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59214106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com