Google 地图 infowindow.setContent 条件

Google Maps infowindow.setContent conditionals

我正在从 JSON 创建一些 Google 地图标记,我想使用 shorthand if 语句来格式化内容。这是我现在拥有的:

infowindow.setContent('<strong>' + this.title + '</strong>' + this.description + '<a href="' + this.link + '" target="_blank">Website</a>');

所有标记都有标题,但有些可能有描述但没有 link 或 link 但没有描述。我怎样才能做到,如果标记有 link 和描述,它会像上面那样,如果它有 link 但没有描述,它会像这样:

infowindow.setContent('<strong>' + this.title + '</strong>' + '<br/>' + '<a href="' + this.link + '" target="_blank">Website</a>');

或者像这样,如果它有描述但没有 link:

infowindow.setContent('<strong>' + this.title + '</strong>' + this.description );

我知道我可以设置 3 个不同的 infowindow.setContent 函数,但我想知道是否有一种方法可以使用内联条件语句来完成一个函数。

你可以使用三元运算符

infowindow.setContent('<strong>' + this.title + '</strong>' + 
    ( this.description =='' ?  ('<br/><a href="' + this.link +
        '" target="_blank">Website</a>') : this.description))  ;