Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Array + Splice + undefined
Old 01-22-2007, 07:38 AM Array + Splice + undefined
numbenator's Avatar
Webmaster Talker

Posts: 516
Location: London
Trades: 0
HOW DO I REMOVE AN ELEMENT FROM AN ARRAY AND THEN RE INDEX THAT ARRAY SO I DONT GET "undefined" IN MY OUTPUT OF THE ARRAY.

IE

3 ARRAY elements defined

item_links[0] = "one"
item_links[1] = "two"
item_links[2] = "three"

outputing the above correctly shows

one
two
three


HOWEVER if I

item_links.splice(0,1); /* remove element 0 */

the output is then

undefined
two
three



cheers
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
 
Register now for full access!
Old 01-22-2007, 08:25 AM Re: Array + Splice + undefined
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
Use 2 arrays to provide a simple swap between the arrays.

Code:
var item_links=new Array();
var temp_array=new Array();

item_links[0] = "one"
item_links[1] = "two"
item_links[2] = "three"

for(i=0;i<item_links.length;i++){
  document.write(i + " " + item_links[i] + "<br />");
}

document.write("<hr />");

temp_array=item_links.splice((0,1));
item_links=temp_array;

for(i=0;i<item_links.length;i++){
  document.write(i + " " + item_links[i] + "<br />");
}
Output:
0 one
1 two
2 three
----------------------------------------------------------------------------------------
0 two
1 three

ibbo
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 01-22-2007, 08:26 AM Re: Array + Splice + undefined
numbenator's Avatar
Webmaster Talker

Posts: 516
Location: London
Trades: 0
Thanks Ibo.
Much appreciated.
Trying out now
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 01-22-2007, 08:32 AM Re: Array + Splice + undefined
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
Well i found some serious problems with it. I thought my browser has gone daft.

It now works in mine without the need to use a temp array(although its good practice to do so). Initially i replicated your code and I too got undefined. Now its rolling back fine and I'm baffled by this behaviour.

Anyway the 2 arrays is a sound programming technique and It works for all sorts of programming tasks.

Even if thsi one is a little wierd.

Ibbo
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 01-23-2007, 06:03 AM Re: Array + Splice + undefined
Skilled Talker

Posts: 96
Name: Tudor Barbu
Trades: 0
Try this method
HTML Code:
<script type="text/javascript">
for( index in item_links ) {
    document.write( index + " " + item_links[index] + "<br />" );
}
</script>
to iterate through your arrays.
__________________

Please login or register to view this content. Registration is FREE
Tudor.b is offline
Reply With Quote
View Public Profile Visit Tudor.b's homepage!
 
Reply     « Reply to Array + Splice + undefined
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33821 seconds with 12 queries