Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Array indexOf() Reference Error?

New Here ,
Apr 18, 2015 Apr 18, 2015

Hello,

I have these simple code lines and on line 20 I'm getting the error in the comment but I cant find the problem in my code

  var directions      = [0,1,2,3];

  var lockedDirection = 0;

  var lastDirection   = 2; // sample code

  switch (lastDirection) {

  case 0:

       lockedDirection = 2;

       break;

  case 1:

       lockedDirection = 3;

       break;

  case 2:

       lockedDirection = 0;

       break;

  case 3:

       lockedDirection = 1;

       break;

  };

  var a = directions.indexOf(lockedDirection); // ReferenceError: Function directions.indexOf is undefined

  directions.splice(a, 1);

TOPICS
Scripting
2.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 18, 2015 Apr 18, 2015

I believe the version of JavaScript AE uses only supports indexOf for strings, not arrays.

Dan

Translate
Community Expert ,
Apr 18, 2015 Apr 18, 2015

I believe the version of JavaScript AE uses only supports indexOf for strings, not arrays.

Dan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 18, 2015 Apr 18, 2015

Ok, so AE JavaScript != JavaScript? Thats a bad implementation. If Adobe wants us to use JavaScript for Scripting then they should implement it the right way . I found a Solution here, had to implement the function myself. Array.prototype.indexOf() - JavaScript | MDN

Btw, do I violate any Copyright with implementing this function in my Script?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2015 Apr 18, 2015

As of the CS6 Scripting Guide, ExtendScript supports the 3rd edition of the ECMA-262 standard (which appears to be at edition 5.1 now).

Dan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 18, 2015 Apr 18, 2015

Yes, and this one is from 1999, now we have 2015

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 03, 2015 May 03, 2015
LATEST

I often add functionality to the built in Javascript objects via their prototype, including indexOf:

/**

* Array.indexOf - Like String.indexOf() but checks for an object in an array

*

* @method

* @param  {Object} obj object to find in the array

* @returns {Number} location in the array of the object, or -1 if not found

*/

if(!Array.prototype.indexOf) {

    Array.prototype.indexOf = function(obj) {

        for(var i=0; i<this.length; i++) {

            if(obj===this) {

                return i;

            }

        }

        return -1;

    };

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines