Archi Forum

Archi Plug-ins => jArchi => Topic started by: Manj75 on June 28, 2021, 14:14:44 PM

Title: Array find not a function using Nashorn ES6
Post by: Manj75 on June 28, 2021, 14:14:44 PM
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.
Title: Re: Array find not a function using Nashorn ES6
Post by: Jean-Baptiste Sarrodie on June 28, 2021, 16:07:10 PM
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
Title: Re: Array find not a function using Nashorn ES6
Post by: Manj75 on June 29, 2021, 13:10:56 PM
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)

Title: Re: Array find not a function using Nashorn ES6
Post by: Jean-Baptiste Sarrodie on June 29, 2021, 14:19:14 PM
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
Title: Re: Array find not a function using Nashorn ES6
Post by: Manj75 on June 29, 2021, 16:24:14 PM
JB, thank you very much - all sorted :)