Array find not a function using Nashorn ES6

Started by Manj75, June 28, 2021, 15:14:44 PM

Previous topic - Next topic

Manj75

I was hoping to use a simple find method for a array but encountering errors.

Initially I was was using:

var idHeader = columnHeaders.find(o => o.header === "Name");

which works fine in GraalVM but not in Nashorn ES6 which I am trying to resolve.

I then went on to try the following:

var idHeader = columnHeaders.find(function( element ) {
element.header === "Name";
});


and get the same error.

Is Array.find() not support in Nashorn ES6?  If they are supported what am I doing wrong? Thanks.

Jean-Baptiste Sarrodie

Hi,

You have to use a polyfill: https://vanillajstoolkit.com/polyfills/arrayfind/

Also, you have to use a function and not an arrow function. Make sure this function returns a boolean (yours is missing the return keyword).

Regards,

JB
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Manj75

Thanks JB - I tried to with the missing return statement but still not working.  Any chance you can reply with the code that will work?

var idHeader = columnHeaders.find( function( element ) {
return element.header === "Name";
});


results in:

TypeError: columnHeaders.find is not a function
at <anonymous> (file:test.ajs:132)


Jean-Baptiste Sarrodie

Hi,

As written, you have to use a polyfill (a polyfill is a piece of code that will add support for a new method into an old engine). So first add the code from https://vanillajstoolkit.com/polyfills/arrayfind . This code will add the Array.find() method. Then you'll be able to use it.

BTW, don't use === (strict equality) but == instead, most of the time you really do want the JS engine to convert things for you before testing for equality (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality ).

Regards,

JB
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Manj75