pushIfNotExists

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

  • SimonJMSimonJM Posts: 5,947

    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.

  • MikeDMikeD Posts: 294

    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.

    var aProperties = Object.getOwnPropertyNames(b);print(JSON.stringify(aProperties,null,"\t"));

     

    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.

    print( "------ Array.pushIfNotExists() ------" );var aTemp = [ "a", "b", "c", "d" ];print( "var aTemp = [ \"a\", \"b\", \"c\", \"d\" ];" );print( "aTemp.length; //", aTemp.length );print( "---" );var a = "e";print( "var a = \"e\";" )print( "---" );var b = aTemp.pushIfNotExists( a );print( "var b = aTemp.pushIfNotExists( a );" );print( "typeof b; //\"" + typeof b + "\"" );print( "Array.isArray( b ); //", Array.isArray( b ) );print( "b.length; //", b.length );print( "b; //", JSON.stringify( b, null, "" ) );print( "---" );b = aTemp.pushIfNotExists( a );print( "b = aTemp.pushIfNotExists( a );" );print( "typeof b; //\"" + typeof b + "\"" );print( "Array.isArray( b ); //", Array.isArray( b ) );print( "b.length; //", b.length );print( "b; //", JSON.stringify( b, null, "" ) );print( "---" );print( "typeof aTemp; //\"" + typeof aTemp + "\"" );print( "Array.isArray( aTemp ); //", Array.isArray( aTemp ) );print( "aTemp.length; //", aTemp.length );print( "aTemp; //", JSON.stringify( aTemp, null, "" ) );print( "---" );print( "b == aTemp; //", b == aTemp );print( "\n\n------ Array.push() ------" );var aTemp = [ "a", "b", "c", "d" ];print( "var aTemp = [ \"a\", \"b\", \"c\", \"d\" ];" );print( "aTemp.length; //", aTemp.length );print( "---" );var a = "e";print( "var a = \"e\";" )print( "---" );var b = aTemp.push( a );print( "var b = aTemp.push( a );" );print( "typeof b; //\"" + typeof b + "\"" );print( "Array.isArray( b ); //", Array.isArray( b ) );print( "b; //", JSON.stringify( b, null, "" ) );print( "---" );b = aTemp.push( a );print( "b = aTemp.push( a );" );print( "typeof b; //\"" + typeof b + "\"" );print( "Array.isArray( b ); //", Array.isArray( b ) );print( "b; //", JSON.stringify( b, null, "" ) );print( "---" );print( "typeof aTemp; //\"" + typeof aTemp + "\"" );print( "Array.isArray( aTemp ); //", Array.isArray( aTemp ) );print( "aTemp.length; //", aTemp.length );print( "aTemp; //", JSON.stringify( aTemp, null, "" ) );print( "---" );print( "b == aTemp; //", b == aTemp );

     

  • Ok thanks. I was kinda hoping that the negative one result would be implemented as it gives a nice test in a loop that you can iterate as the current data value is not unique. Anywhos,can't cry over spilt milk.
  • Ok thanks. I was kinda hoping that the negative one result would be implemented as it gives a nice test in a loop that you can iterate as the current data value is not unique. Anywhos,can't cry over spilt milk.

    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.

Sign In or Register to comment.