pushIfNotExists
RajDarge
Posts: 17
var aTemp = [ "a", "b", "c", "d" ];var a = "e";var b = aTemp.pushIfNotExists( a );// aTemp == [ "a", "b", "c", "d", "e" ]print(b);b = aTemp.pushIfNotExists( a );print(b);print(aTemp);
the ouput of the above statement should be (if I am reading things right)
5
-1
a,b,c,d,e
but I get:
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
am I doing something wrong or is there a bug?
Comments
Having had a quick look at the documentation, that'd be my assumption too - except not sure what the return would be if the elelment already exists, that is not mentioned.
Actually in both cases it returns the array. Propably is something Rob haven't change yet in the documentation.
Place the next code after each b=.... to see the results.
Use the b.length to take the array's new length.
Rob has provided a nice little demo of how it does work, which is not how the docs say it does. In fact it rturns the modified array. Because changing the behaviour to match the docs would break existing scripts (I've used this more than once) the docs will be updated to match the implemented behaviour.
If an item has been added then the length will increase, you could test for that - not as elegant as just checking for a negative return, but I think it would be reliable.