galvin Posted May 1, 2009 Share Posted May 1, 2009 If you have an array with 10 elements and you use splice() to remove the 3rd element (index of 2), it changes the index of all the ones after the one you removed. Is there a way to make it so the indexes do NOT change at all? So if I had an array of.... var arr = new Array(10); arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; arr[3] = "Jim"; arr[4] = "Borge"; arr[5] = "Mike"; arr[6] = "Joe"; arr[7] = "Cathy"; arr[8] = "Sarah"; arr[9] = "Lauren"; ANd then I use splice() to remove "Jim", I'd like the array to now be... arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; arr[4] = "Borge"; arr[5] = "Mike"; arr[6] = "Joe"; arr[7] = "Cathy"; arr[8] = "Sarah"; arr[9] = "Lauren"; There's gotta be a way, right? Quote Link to comment https://forums.phpfreaks.com/topic/156346-solved-not-change-indexes-when-using-splice/ Share on other sites More sharing options...
fry2010 Posted May 1, 2009 Share Posted May 1, 2009 could you instead overwrite that array? so arr[3] = ''; What specifically u looking for? Or you could try: var max_array = arrayMaximum(arr[]); var array_split_value = 3; for(z = array_split_value; z < max_array; z++) { arr[z] = arr[z+1]; } You might not be able to do that exactly as Iv done it, im not familiar with js. Quote Link to comment https://forums.phpfreaks.com/topic/156346-solved-not-change-indexes-when-using-splice/#findComment-823174 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 Just set the one you want to remove to undefined. arr[#] = undefined; Quote Link to comment https://forums.phpfreaks.com/topic/156346-solved-not-change-indexes-when-using-splice/#findComment-823243 Share on other sites More sharing options...
galvin Posted May 3, 2009 Author Share Posted May 3, 2009 I can't make it "undefined" because I need to continue to display the array values on the webpage. After setting them to "undefined", they then display the actual word "undefined" on the page. Good idea, but it won't work for my purposes unfortunately. I feel like there has to be a way to tell javascript to NOT change the array indexes even after you remove one using Splice(), but I am not familiar enough with Javascript to know how. If anyone else has any ideas let me know. Thanks, Greg Quote Link to comment https://forums.phpfreaks.com/topic/156346-solved-not-change-indexes-when-using-splice/#findComment-825097 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 I said undefined, not the string "undefined". By default, arr[#] is undefined if not defined. Learn JavaScript. Test it out. Also, if you're going to print it out using a loop, you can check for that case even if it is the string undefined, can't you? Quote Link to comment https://forums.phpfreaks.com/topic/156346-solved-not-change-indexes-when-using-splice/#findComment-825099 Share on other sites More sharing options...
galvin Posted May 3, 2009 Author Share Posted May 3, 2009 I was only using quotes in that post, sorry. It was set to undefined in my code (not a string). But it still made the word "undefined" print on the screen. Quote Link to comment https://forums.phpfreaks.com/topic/156346-solved-not-change-indexes-when-using-splice/#findComment-825100 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 I don't know how you're printing it out, but you should do something like this: var e = new Array(); e[3] = 4; var start = 0; while (e[start++] !== undefined) { document.body.innerHTML += e[start]; } Do know that it's an infinite loop. It's just for hints. Quote Link to comment https://forums.phpfreaks.com/topic/156346-solved-not-change-indexes-when-using-splice/#findComment-825107 Share on other sites More sharing options...
galvin Posted May 3, 2009 Author Share Posted May 3, 2009 Ill give that type of thing a try, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/156346-solved-not-change-indexes-when-using-splice/#findComment-825201 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.