0

I would like to filter an array in my XPage with serverside Javascript. Unfortunately I get the following error:

Error calling method 'filter(Function)' on an object of type 'Array [JavaScript Object]'

I have an Array of Strings like ["elem1","elem2","elem3"]

I call the function like this:

list.filter(function(){

});

Is there any reason why this error happens? Does this function even exist in ssjs?

This question is not duplicate since it is not clear that Xpages/Lotus Notes runs Rhino in background.

Max
  • 1,053
  • 1
  • 13
  • 34

2 Answers2

1

It sounds like whatever server-side JavaScript environment you're using doesn't support ES5 features (that's features from the 5th edition specification from December 2009).

You can use a polyfill for that and other things that were added to Array, see MDN, but beware: If ES5 features aren't supported, it's impossible to add things to Array.prototype without making them enumerable, meaning any code (mis)using for-in to loop through arrays will be affected.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Accepted as the polyfill did the trick for me. Just put it into a ssjs Script Library and include it into your page. Nevertheless thanks for all the other comments and advices! – Max Nov 04 '16 at 08:54
0

There is Rhino behind XPages. And this SO topic No Array.filter() in Rhino? says, it is out of date.

Edit: No, it is not. Years ago I read somewhere about it. Now it seems it was not true. According to comment by Dan Sickles (quoting Philippe Riand?) here:

It runs on the server jvm and uses javascript as the application language. For licensing reasons, IBM wrote their own jvm javascript engine instead of using Rhino. With Rhino shipping in java 6, they should be able to ship it in Designer 8.5 (or later). The licensing problems may have been around the extensions like @Formulas and type declarations. Classes, modules/namespaces and type declarations are coming in javascript 2 and even google is helping to get that implemented in Rhino. I'd hate to see a non-standard, javascript engine underlying the coolest web development technology in Domino.

Speaking of Rhino, the "most important new feature that is not as certain to be in 8.5 as XPages" uses Rhino and other jvm scripting languages on the client. If this makes it into the product, two years from now most new Notes applications will be written in neither Lotusscript nor Java. I'll leave it at that.

In fact, there are few topics how to use Rhino in XPages, so with newest Rhino version your code would work. Anyway, my advice is to use Java calls.

Community
  • 1
  • 1
Frantisek Kossuth
  • 3,524
  • 2
  • 23
  • 42
  • I'm not finding corroboration that ssjs uses Rhino by default. I'm finding things like [this](http://dontpanic82.blogspot.co.uk/2010/05/using-rhino-javascript-engine-in-xpages.html) (linked from [here](http://stackoverflow.com/questions/26695434/how-to-clean-ssjs-in-domino-server-after-someone-used-javascript-prototype-in-a)) and [this](https://en.wikipedia.org/wiki/Comparison_of_server-side_JavaScript_solutions) which suggest it doesn't. And Rhino's had `Array#filter` for **years**. Can you cite a source? – T.J. Crowder Nov 04 '16 at 08:25