473,796 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript Array, Splice

6 New Member
So currently i have an application i developed some time ago that has 2 arrays...
FirstName
LastName

this application randomly goes through the arrays displaying the names as it goes and finally stops on one (its a prize drawing application we use to distribute prizes at company parties)

when a name gets drawn it clears that array value... if the random loop lands on any empty spot it starts over... the problem with this is, after many names have been cleared it starts to repeat itself so much it runs very slow....

So i have been looking for a way to delete the array value all together such as .splice however it isnt working for me so basically if this is my array

FirstName[0] = "Josh"
FirstName[1] = "Bob"
FirstName[2] = "Tim"
FirstName[3] = "Ashley"
FirstName[4] = "Frank"

and i say... FirstName.Splic e(0,1)

Then the array would be like below...
FirstName[0] = "Bob"
FirstName[1] = "Tim"
FirstName[2] = "Ashley"
FirstName[3] = "Frank"


anyone know how i can achieve this?
Jan 25 '08 #1
3 3496
mrhoo
428 Contributor
splice returns the item(s) that get removed, so you can select and remove the selected item from the array in the same statement.

You'd use something like this to deal random cards from a deck, for example, but it should work for your purpose.

Expand|Select|Wrap|Line Numbers
  1. var L=FirstName.length,f;
  2. while(--L){
  3.     f=FirstName.splice(Math.floor(Math.Random()*L),1);
  4. //write the name
  5. }
  6.  
Jan 25 '08 #2
DeanJo
6 New Member
Well i need to remove the item from the array and then still be able to execute the same functions that search through the array. Ideally delete something then re-index the array with the data that still exists...
Jan 25 '08 #3
mrhoo
428 Contributor
Then make an array of integers the length of your name list,
with each item a subsequent integer.
Shuffle the array and shift or pop your way through it,
using the integers returned as your name array indexes.

Expand|Select|Wrap|Line Numbers
  1. var L= FirstName.length;
  2. var i= 0,A=[];
  3. while(i<L) A[i]= i++;
  4. A.sort(function(){
  5.     return  .5 - Math.random();
  6. })
  7. while(A.length){
  8.     f= FirstName(A.pop());
  9.     //do whatever with f
  10. }
  11.  
Jan 25 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2425
by: Tek9_AK | last post by:
I need to find a way to transfer all the values of an array inside a function out of the fuction into another array. IE function splice($filename){ if(file_exists($filename)){ $value=array("0"); // clear the array. $rows=file($filename); // break text file into a rows array for($num=0;$num < count($rows); ++$num){ $column=split(";",$rows); $value=$column;}}
5
6387
by: effendi | last post by:
I wrote a simple script to remove an element of an array but I don't think this is the best way to go about it. I have a list of about five elements seperated by ";" I split the array using array.split(";") command and proceeded to update the elemment by assigning the null value to the arrayindex array=""
15
3336
by: alanbe | last post by:
Greetings I am making a flashcard type application to help me in my TCP/IP protocols test. My instructor will test us periodically on how a device or networking function relates to the OSI layer. EG. bits-layer 1. Any way, I want the quiz to reorder the problems each time I take it. Here is part of the code i did so far for 62 components in the quiz.
2
5235
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it down to a very simple test case which can be cut-n-pasted into a .html file and viewed in a browser: ============================================================================
1
3378
by: anniefs | last post by:
hi help me i m so much stuck int he code and i have no time .... i used ASP VBscipt and javascript functions with MS database javascript function add records in MS DB by using ASP vbscript recordset varible when user click the save button then some values save in the database so i write javascript function to add the records... here is the code
1
2990
by: agendum97 | last post by:
MSDN says splice has the following arguments: arrayObj.splice(start, deleteCount, ]]]) Thus I can insert items into an array using splice. But how do I insert an entire array? For example: var arr1 = ; arr1 = "a"; arr2 = "d";
18
1845
by: dhtml | last post by:
Array.splice({}) What should it do? I think it should return a new Array with length 0. Array.splice(arr, start, deleteCount ]]) http://bclary.com/2004/11/07/#a-15.4.4.12 Example:
2
9041
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to figure this problem out. Any suggestions? thanks Joel
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.