首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将第二项添加到JSON数组中,只在有第二项的情况下?

如何将第二项添加到JSON数组中,只在有第二项的情况下?
EN

Stack Overflow用户
提问于 2019-02-20 17:50:57
回答 1查看 138关注 0票数 1

我有一些链接数据JSON,我想知道如何向数组中添加第二个条目,只要有第二个条目。

在我的例子中,在一篇文章中添加一个或两个合作者。

代码语言:javascript
复制
  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}"
    }

这里的模板语言是帕格。

那我该怎么加第二个人呢?

代码语言:javascript
复制
     "sameAs": !{JSON.stringify(article.author && article.author.sameAs)}
  }
  if (coauthor) return ,{author-2}
  ],
     "datePublished": "#{article.date}",
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-21 06:35:53

一种(稍微有点迂回的)方法是使用未缓冲的代码块预先在javascript中构建模式对象,然后使用JSON.stringify()将其输出到脚本标记中。

假设您的数据是结构化的,那么article.authors是一个作者数组:

代码语言:javascript
复制
-
  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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54792473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档