Ralf_Reddings
Ralf_Reddings

Reputation: 1555

Is there a method for checking if an array has a certain element in it?

In this instance, I want to find out if a array has a element in it:

function OnTestCmd(scriptCmdData){
    print(["Jenny", "Matilda", "Greta"].indexOf("Matilda"))     //Error: Object doesn't support this property or method (0x800a01b6)
    print(["Jenny", "Matilda", "Greta"].includes("Matilda"))   //Error: Object doesn't support this property or method (0x800a01b6)
    
    arr = new Array("Jenny", "Matilda", "Greta")
    print(arr.indexOf("Greta"))                               //Error: Object doesn't support this property or method (0x800a01b6)
    }

I am told I can use includes() or indexOf() but neither seem to work

Looking at MS docs for Jscript, there does not seem be a includes() method but it does have indexOf() which just errors out.

Edit1: The print function is not complicated at all and I dont think its the issue, here it is, the DOpus.Output() method is documented here:

    function print(str){
        DOpus.Output(str)
    }

Upvotes: 2

Views: 85

Answers (1)

l3l_aze
l3l_aze

Reputation: 613

JScript doesn't have Array.indexOf, but it has String.indexOf, which is likely why there's confusion.

Could use a ponyfill to provide the necessary functionality without modifying Array, which is safest.

function arrayIndexOf (list, element) {
  for (var i = 0; i < list.length; i++) {
    // Should do more to confirm equality.
    // Can't use === though.
    if (list[i] == element) {
      return i
    }
  }

  return -1
}

Upvotes: 2

Related Questions