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?
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);
Splice returns an array.
Try:
test.splice( 2, 0, test.splice(3, 1)[0] );
gah, so obvious -thanks 
Sorry, you must be a member to post to a conversation. Either
log in or
sign up
to get involved.