TwelvestoneFront End

Javascript splice doesn't return objects?


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
DontBogartMe
 
2011-01-23

what's up with this?

function testCrap(name){
    this.name = name;
}

var test = new Array(new testCrap("a"), new testCrap("b"), new testCrap("c"), new testCrap("d"), new testCrap("e"));

var dump = '';
for(x in test){
    if(dump)dump += ', ';
    dump += test[x].name;
}
document.write("<p>" + dump + "</p>");

test.splice(2, 0, test.splice(3, 1));

dump = '';
for(x in test){
    if(dump)dump += ', ';
    dump += test[x].name;
}
document.write("<p>" + dump + "</p>");

I'm trying to swap the positions of two items in the array. This code works fine if you use simple strings, but if you store objects in the array it returns "undefined".

Any idea?

DontBogartMe
 
2011-01-23

doing it the long way works, but I did like my little splice line :(

var idx = 3;
var target = test[idx];
test.splice(idx, 1);
test.splice(idx - 1, 0, target);
wowbagger[tip]
 
2011-01-23

Splice returns an array. k Try:

test.splice( 2, 0, test.splice(3, 1)[0] );

DontBogartMe
 
2011-01-23

gah, so obvious -thanks k

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFront End

Javascript splice doesn't return objects?