为什么 jsonfn.expand 包含空值?
Why does jsonfn.expand include null values?
我正在使用 jsonfn 构建一个 JSON 对象数组,并将它们呈现在 JavaScript window 对象上。
[#assign json = []]
[#list articles as article]
[#assign json += [jsonfn.from(article).add("categories", "title", "@name").expand("categories").inline().print()]]
[/#list]
<script>
window.articles = [${json?join(",")}];
</script>
虽然有效,但扩展的类别字段 (expand("categories")
) 有时会包含空值。
{
"categories": [
{
"displayName": "Example Category",
"@name": "example-category"
},
null
],
"title": "Example Article",
"@name": "example-article"
}
这要求我在 JavaScript 中过滤时添加一个 instanceof Object
检查,这样我就不会得到空错误。
export const filterArticles = (selectedCategory, articles) => {
return articles.filter((article) => {
return article.categories.find((category) =>
category instanceof Object && category['@name'] === selectedCategory
);
});
};
为什么jsonfn.expand
有时会输出空值?为什么没有空安全检查来避免将它们包含在呈现的输出中?
我正在使用 jsonfn 构建一个 JSON 对象数组,并将它们呈现在 JavaScript window 对象上。
[#assign json = []]
[#list articles as article]
[#assign json += [jsonfn.from(article).add("categories", "title", "@name").expand("categories").inline().print()]]
[/#list]
<script>
window.articles = [${json?join(",")}];
</script>
虽然有效,但扩展的类别字段 (expand("categories")
) 有时会包含空值。
{
"categories": [
{
"displayName": "Example Category",
"@name": "example-category"
},
null
],
"title": "Example Article",
"@name": "example-article"
}
这要求我在 JavaScript 中过滤时添加一个 instanceof Object
检查,这样我就不会得到空错误。
export const filterArticles = (selectedCategory, articles) => {
return articles.filter((article) => {
return article.categories.find((category) =>
category instanceof Object && category['@name'] === selectedCategory
);
});
};
为什么jsonfn.expand
有时会输出空值?为什么没有空安全检查来避免将它们包含在呈现的输出中?