Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Arrays


Message #1 by Greg Griffiths <greg.griffiths@g...> on Tue, 30 Apr 2002 20:20:41 +0100
Heres some array functions from Wrox Professional Javascript book

function Array_push(items)
{
  for(var x=0;x<arguments.length;x++)
    this[this.length]=arguments[x];
  return this.length;
}

if(!Array.prototype.pop)
  Array.prototype.pop=Array_pop;

function Array_pop()
{
  var old=this[this.length-1];
  delete this[this.length-1];
  this.length--;
  return old;
}

if(!Array.prototype.splice)
  Array.prototype.splice=Array_splice;

function Array_splice(start,deleteCount,items)
{
  var results = new Array();
  //copy items to delete and store in return array
  for(var x=0; x<deleteCount; x++)
    results[x] = this[x + start];
  //shift all remaining elements down
  for(x=start; x<this.length - deleteCount; x++)
    this[x] = this[x + deleteCount];
  //remove elements from the end
  this.length -= deleteCount;
  if(arguments.length>2)
  {
    //make room for the provided items to be inserted
    for(x=this.length-1; x>= start; x--)
      this[x + deleteCount] = this[x];
    //insert the provided items
    for(x=0; x<deleteCount; x++)
      this[x + start] = arguments[x + 2];
  }
  return results;
}

if(!Array.prototype.shift)
  Array.prototype.shift=Array_shift;

function Array_shift()
{
  var old=this[0];
  for(var x=0;x<this.length-1;x++)
    this[x]=this[x+1];
  delete this[this.length-1];
  this.length--;
  return old;
}

if(!Array.prototype.unshift||Array(6,6,6,6).unshift(4)!=5)
	Array.prototype.unshift=Array_unshift;

function Array_unshift(items)
{
  //move items up the chain
  for(var x=this.length-1;x>=0;x--)
    this[x+arguments.length]=this[x];
  for(x=0;x<arguments.length;x++)
    this[x]=arguments[x];

  return this.length;
}

-----Original Message-----
From: Greg Griffiths [mailto:greg.griffiths@g...]
Sent: 30 April 2002 20:21
To: javascript
Subject: [javascript] Arrays


Dear All,
	I'm trying to add data to an array in Javascript using code like :

	var a = new Array(1,2,3,4)

	if (needExtra==true)
	{
		a.concat(5,6);
	}

but it fails to work, I can't use the Push/Pop oir the Shift/Unshift as 
they only work in NN any ideas ?



---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
r-20

________________________________________________________________________
Scottish Enterprise Network
http://www.scottish-enterprise.com

Headquarters Address & Contact Numbers

150 Broomielaw
5 Atlantic Quay
Glasgow
G2 8LU.
Tel:  +44 (0) 141 248 2700.
Fax:  +44 (0)141 221 3217

 This message is sent in confidence for the addressee only.
It may contain legally privileged information. The contents are not to
be disclosed to anyone other than the addressee. Unauthorised recipients
are requested to preserve this confidentiality and to advise the sender
immediately of any error in transmission.



  Return to Index