Why does Array.prototype.filter() throw an error in Magnolia JavaScript models?

后端 未结 1 1179
梦毁少年i
梦毁少年i 2021-01-28 01:37

I\'m attempting to filter a FreeMarker list in a Magnolia JavaScript model using Array.prototype.filter().

List

[#assign list = [1, 2, 3         


        
相关标签:
1条回答
  • 2021-01-28 01:46

    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)
    

    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();
    
    0 讨论(0)
提交回复
热议问题