array.includes() does not work?

yc
yc Posts: 41 🔥

sanity check... does checking if an array contains an element using array.includes("test") not work in LensScript?

`TypeError: undefined not callable (property 'includes' of [object Array])'

Answers

  • Joshua Beckwith
    Joshua Beckwith Posts: 18 🔥

    LS uses a super ancient version of ecmascript, so it doesn't support a lot of the new language features. You could find polyfills for some of the prototype functions, but using keywords like const and class will require you to transpile down to ES3 or something.

    https://stackoverflow.com/questions/53308396/how-to-polyfill-array-prototype-includes-for-ie8

  • In some cases, you can workaround with array.indexOf("test"), and if it returns -1, it's like getting "false" returned from array.includes("test").

  • yc
    yc Posts: 41 🔥

    It would be nice to understand which version of EMCAscript LS uses
    TBH many of my hurdles and hours wasted so far has been in understanding what Lens Javascript does and does not support.

  • yc
    yc Posts: 41 🔥

    @Michael French said:
    In some cases, you can workaround with array.indexOf("test"), and if it returns -1, it's like getting "false" returned from array.includes("test").

    You're right - that's a simple ==-1 or not fix... However, there are many other array operations one has taken for granted in standard Javascript that don't seem to work.

  • mickys
    mickys Posts: 2
    edited November 20 #6

    No, the array.includes() method is not supported in LensScript. LensScript uses an older version of JavaScript that does not have this method. You can use the array.indexOf() method instead.

    To check if an array contains an element using indexOf(), you would Magazine the following code:

    JavaScript
    if (array.indexOf("test") !== -1) {
    // element is in the array
    } else {
    // element is not in the array
    }