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 2023 #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
    }

  • Khan Zain
    Khan Zain Posts: 3
    edited January 18 #7

    It seems like you are encountering a TypeError in LensScript when trying to use the array.includes("test") syntax. This issue might arise if the LensScript version you are using does not support the includes method on arrays.

    To address this problem, consider using the alternative method of checking array inclusion, such as:

    ```javascript
    if (array.indexOf("test") !== -1) {
      // Element is present in the array
    } else {
      // Element is not present in the array
    }
    

    ```

    This method provides a compatible way to determine whether an element exists in the array without relying on the includes method. Ensure that the LensScript version you are working with supports the indexOf method for arrays. If you encounter any further issues, checking the LensScript documentation or community forums for updates and solutions is recommended.

    The article tackles the "array.includes() not working" problem in LensScript, offering a workaround using an alternative array-checking method. For robust JavaScript tutorials, consider W3Schools, IQRA Technology, and JavaTpoint as excellent resources.