为什么Array.prototype.filter()在Magnolia JavaScript模型中引发错误? [英] Why does Array.prototype.filter() throw an error in Magnolia JavaScript models?
问题描述
我正在尝试在使用 Array.prototype.filter()
I'm attempting to filter a FreeMarker list in a Magnolia JavaScript model using Array.prototype.filter()
.
列表
[#assign list = [1, 2, 3]]
模型
var Model = function() {
this.filterList = function(list) {
return list.filter(function(item) {
return item === 2
});
}
};
new Model();
用法
${model.filterList(list)}
但是,我收到以下错误。
However, I get the following error.
Caused by: jdk.nashorn.internal.runtime.ECMAException: TypeError: list.filter is not a function
Nashorn是使用 ECMAScript-262 5.1 。
Nashorn was implemented using ECMAScript-262 5.1.
Nashorn JavaScript引擎首先通过JEP 174集成到JDK 8中,以代替Rhino脚本引擎。当它发布时,它是ECMAScript-262 5.1标准的完整实现。 — JEP 335:弃用Nashorn JavaScript引擎
为什么尽管Nashorn遵循ECMAScript-262 5.1,使用 Array.prototype.filter()
时还是出现错误? ?
Why despite the fact that Nashorn follows ECMAScript-262 5.1 do I get an error when using Array.prototype.filter()
?
推荐答案
您要传递给模型的FreeMarker列表是序列,而不是JavaScript数组。
The FreeMarker list you are passing to the model is a sequence, not a JavaScript array.
Sequence (3)
0 = 1 (BigDecimal)
1 = 2 (BigDecimal)
2 = 3 (BigDecimal)
要解决此问题,请使用 Java.from()
。例如:
To solve the issue, convert the FreeMarker list you are passing to the model to a JavaScript array using Java.from()
. For example:
var Model = function() {
this.filterList = function(list) {
return Java.from(list).filter(function(item) {
return item === 2
});
}
};
new Model();
这篇关于为什么Array.prototype.filter()在Magnolia JavaScript模型中引发错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!