我正在使用 Europeana API 用某些照片填充我的网站。我已经决定要检索哪些元数据,但是当我意识到并非所有照片都具有我想要的元数据并导致错误和未定义时遇到了问题。
我如何调整它,以便仅当正在检索的照片存在 edmPlaceLabel 时才显示它?如果它不存在,只需跳过它并继续。
data.items.forEach((item) => {
const output = document.querySelector('#output');
const title = item.title,
year = item.year,
provider = item.provider,
dataProvider = item.dataProvider,
LabelLangAware = item.edmConceptPrefLabelLangAware,
edmPlaceLabel = item.edmPlaceLabel[0],
edmPlaceLabelLangAware = item.edmPlaceLabelLangAware.hr[0],
edmTimespanLabel = item.edmTimespanLabel[30].def,
edmTimespanLabelLangAware = item.edmTimespanLabelLangAware.hr,
europeanaCollectionName = item.europeanaCollectionName[0],
link = item.guid,
thumbnail = item.edmPreview[0],
edmIsShownBy = item.edmIsShownBy;
output.innerHTML += `${index}
<a href="${link}">Link: ${title}</a>
<br>
<h4>${title}</h4>
<p>Godina: ${year}</p>
<p>Izvor: ${dataProvider}</p>
<p>Opis: ${LabelLangAware}</p>
<p>Mjesto: ${edmPlaceLabel}</p>
<p>Mjesto: ${edmPlaceLabelLangAware}</p>
<p>${edmTimespanLabel}</p>
<p>${edmTimespanLabelLangAware}</p>
<p>Provider: ${provider}</p>
<p>Kolekcija: ${europeanaCollectionName}</p>
<br>
<img src="${thumbnail}" alt="${title}">
<br>
<p>Link na izvor: ${edmIsShownBy}</p>
`;
});
尝试使用三元表达式
例子:
var val = obj ? obj.prop : '';
是相同的:
var val = null;
if (obj) {
val = obj.prop;
} else {
val = ''
}
也可以与字符串变量一起使用:
`hello ${val ? val : ''} world`
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句