仅在存在第二项的情况下,如何将其添加到JSON数组中?

时间:2019-02-20 17:50:57

标签: json pug

我有一些Linked Data JSON,我想知道如何在且仅当存在第二个项目时才将第二个项目添加到数组中。

就我而言,在一篇文章中添加一位或两位共同作者。

  script(type="application/ld+json").
    {
      "@context": "http://schema.org",
      "@type": "Article",
      "headline": "#{article.title}",
      "description": "#{article.description}",
      "author": [{
        "@type": "Person",
        "name": "#{article.author && article.author.name}",
        "givenName": "#{article.author && article.author.givenName}",
        "additionalName": "#{article.author && article.author.additionalName}",
        "familyName": "#{article.author && article.author.familyName}",
        "email": "#{article.author && article.author.email}",
        "jobTitle": "#{article.author && article.author.jobTitle}",
        "workLocation": "#{article.author && article.author.workLocation}",
        "worksFor": "Company",
        "image": "#{article.author && article.author.image}",
        "url": "#{article.author && article.author.url}",
        "sameAs": !{JSON.stringify(article.author && article.author.sameAs)}
      }],
      "datePublished": "#{article.date}",
      "dateModified": "#{article.updated || article.date}",
      "publisher": {
        "@type": "Organization",
        "name": "Company",
        "logo": {
          "@type": "ImageObject",
          "url": "...logo-url.jpg"
        }
      },
      "image": "#{article.image}",
      "url": "#{article.url}"
    }

这里的模板语言是哈巴狗。

那我该如何添加第二个人?

     "sameAs": !{JSON.stringify(article.author && article.author.sameAs)}
  }
  if (coauthor) return ,{author-2}
  ],
     "datePublished": "#{article.date}",

1 个答案:

答案 0 :(得分:1)

一种方法(略为回旋)是事先使用无缓冲代码块在javascript中构建架构对象,然后使用JSON.stringify()将其输出到脚本标签中。

假设您的数据的结构使得article.authors是一个作者数组:

-
  let jsonld = {
    '@context': 'http://schema.org',
    '@type': 'Article',
    'headline': article.title,
    'description': article.description,
    'datePublished': article.date,
    'dateModified': article.updated || article.date,
    'publisher': {
      '@type': 'Organization',
      'name': 'Company',
      'logo': {
        '@type': 'ImageObject',
        'url': '...logo-url.jpg'
      }
    },
    'image': article.image,
    'url': article.url
  }
  for (let i = 0; i < article.authors.length; i++) {
    jsonld.authors = jsonld.authors || []
    let author = {
      '@type': 'Person',
      'name': article.authors[i].name,
      'givenName': article.authors[i].givenName,
      'additionalName': article.authors[i].additionalName,
      'familyName': article.authors[i].familyName,
      'email': article.authors[i].email,
      'jobTitle': article.authors[i].jobTitle,
      'workLocation': article.authors[i].workLocation,
      'worksFor': 'Company',
      'image': article.authors[i].image,
      'url': article.authors[i].url,
      'sameAs': JSON.stringify(article.authors[i].sameAs
    }
    jsonld.authors.push(author)
  }

script(type="application/ld+json")= JSON.stringify(jsonld, null, 2)