【问题标题】:How to add a second item to a JSON array, only if there is one?如何将第二项添加到 JSON 数组中,仅当有一个?
【发布时间】:2019-02-20 17:50:57
【问题描述】:

我有一些关联数据 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}"
    }

这里的模板语言是 pug。

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

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

【问题讨论】:

    标签: json pug


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多