Author Topic: Array find not a function using Nashorn ES6  (Read 228 times)

Manj75

  • Senior Member
  • ****
  • Posts: 176
Array find not a function using Nashorn ES6
« on: June 28, 2021, 15:14:44 PM »
I was hoping to use a simple find method for a array but encountering errors.

Initially I was was using:

Code: [Select]
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:

Code: [Select]
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 729
  • Archi Evangelist and Contributor. Visionary.
    • About me...
Re: Array find not a function using Nashorn ES6
« Reply #1 on: June 28, 2021, 17: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
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Manj75

  • Senior Member
  • ****
  • Posts: 176
Re: Array find not a function using Nashorn ES6
« Reply #2 on: June 29, 2021, 14: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?

Code: [Select]
var idHeader = columnHeaders.find( function( element ) {
return element.header === "Name";
});

results in:

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

Jean-Baptiste Sarrodie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 729
  • Archi Evangelist and Contributor. Visionary.
    • About me...
Re: Array find not a function using Nashorn ES6
« Reply #3 on: June 29, 2021, 15: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
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Manj75

  • Senior Member
  • ****
  • Posts: 176
Re: Array find not a function using Nashorn ES6
« Reply #4 on: June 29, 2021, 17:24:14 PM »
JB, thank you very much - all sorted :)