1

So here's what I'm trying to do in pseudocode:

array = new Array();
thisObj = new objectTypeOne();
thisObj2 = new objectTypeTwo();
array.push(thisObj);
array.push(thisObj2);

for( i=0; i<=array.length, i++)
{ 
  if( array[i] == objectTypeOne() )
  {
    //do code
  }
 }

I know I could have two different arrays for each object type but that would ruin a lot of my other code which assumes they're both in the same array. (they are pratically same object with slight but vital differences, I suppose I should really have objectTypeTwo derive from other, but that's irrelevant at the moment).

2
  • I don't understand, what exactly do you want to do? You need to find what object type is current object when you loop through array? You can use if (array[i] is objectTypeOne) //code Commented Jan 17, 2012 at 9:25
  • See the last comment I made to you on my answer. It means you have an undefined reference pushed into your array... which shouldn't happen but you can account for these things with a little error checking to deal with them at runtime. Commented Jan 17, 2012 at 9:44

2 Answers 2

1

I believe what you're looking for is:

if(array[i] is MyObjectType) {
   //I found an instance of MyObjectType
}

The operator "is" does runtime type analysis and will return true if the object you are testing (in this example, array[i]) is either directly of the type you're comparing to or a subclass (in this example, MyObjectType). You might also want to investigate using typeof. However, try to avoid having a TON of calls like this or calls using "typeof"... looking up type information at runtime is costly in any language.

Sign up to request clarification or add additional context in comments.

6 Comments

P.S. - I can see you're still pretty new so welcome to StackOverflow. :)
Thanks for your descriptive answer (and thank you, I'm loving StackOverflow so far). I just have one error:
If I use: if(array[i] is Object()) It says: Incorrect Arguments, Expected one. If I use: if(array[i] is Object) It gives error "term is undefined and has no properties" at runtime
That's weird.... unless you have a reference in the array that is undefined, which is possible. Try changing the if statement to if(array[i] != undefined || array[i] is Object).
Nope still says something is undefined :S
|
1

First correct this i<=array.length to i<array.length. That is the reason for "term is undefined" error.

The solution suggested by @Ascension Systems can be used when you create the instance of diffrent Classes, like

array = new Array();
thisObj = new Student();
thisObj2 = new Employee();
array.push(thisObj);
array.push(thisObj2);

then you can check like

for( i=0; i<array.length, i++)
{ 
  if( array[i] is Student )
  {
    //do code
  }else if(array[i] is Employee){

  }
}

If you are using custom Objects, do like this

array = new Array();
thisObj = new Object();
thisObj.type = "type1";
....
....
thisObj2 = new Object();
thisObj2.type = "type2";
...
...
array.push(thisObj);
array.push(thisObj2);

for( i=0; i<array.length, i++)
{ 
  if( array[i].type == "type1" )
  {
    //do code
  }else if( array[i].type == "type2"){

  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.