Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 1st, 2004, 10:05 PM
Authorized User
 
Join Date: Oct 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default Array Element

Hi there! Anyone know how to import existing element to an array?

example:

existing element already have in a variable:
var temp = ("a", "b", "c");

i want to import the element from variable "temp" into array "char"

var char = new Array (temp); //error! i want this format
var char = new Array ("a", "b", "c"); //default array

It is possible to do that? Thanks a million! I'm appreaciate all kind of help.






 
Old November 2nd, 2004, 04:13 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Some ideas, you will need to imlplement print function, i.e. change it to "alert" if running in a browser:
Code:
var temp = new Array("a", "b", "c");
var copy = temp;
//For simple string arrays:
//var clone = temp.join("|").split("|");
//For more complex arrays:
var clone = new Array();
for (var i = 0; i < temp.length; i++)
{
  clone[i] = temp[i];
}
print("temp: " + temp.toString());
print("copy: " + copy.toString());
print("clone: " + clone.toString()); 
copy[0] = "d";
print("temp: " + temp.toString());
print("copy: " + copy.toString());
print("clone: " + clone.toString()); 
clone[0] = "e";
print("temp: " + temp.toString());
print("copy: " + copy.toString());
print("clone: " + clone.toString()); 

var arr1 = new Array(["a", 0], ["b", 1], ["c", 2]);
var arr2 = arr1;
var arr3 = new Array();
for (var i = 0; i < arr1.length; i++)
{
  arr3[i] = arr1[i];
}
print("arr1: " + arr1.toString());
print("arr2: " + arr2.toString());
print("arr3: " + arr3.toString());
arr2[0] = ["d", 4];
print("arr1: " + arr1.toString());
print("arr2: " + arr2.toString());
print("arr3: " + arr3.toString());
--

Joe
 
Old November 12th, 2004, 04:50 AM
Authorized User
 
Join Date: Oct 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe,
   Thanks.






Similar Threads
Thread Thread Starter Forum Replies Last Post
div element in array won't display onload linus9 BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 5 November 6th, 2008 02:08 AM
first element in array nesting siblings rjonk XSLT 1 May 18th, 2008 03:53 PM
check if an array element is not undefined crmpicco Javascript How-To 1 October 6th, 2005 11:40 AM
How do I test for an empty array element kmoran Excel VBA 1 October 8th, 2004 03:34 AM
Removing an Element from an Associative Array nick8245 Javascript 2 September 26th, 2003 12:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.