PDA

View Full Version : select an array value from within an array



sniperman
10-31-2010, 10:33 AM
There is a snip of code I am trying to optimize and have gone in a new direction. I used to use the split method to spill the contents of an array into a temporary variable as a csv file to access each individual item in that array. Now I want to create one array with many values inside as below.


<script type="text/javascript">
myDB = new Array()
item1="1";
item2="2";
item3="3";
item4="4";

myDB.push(item1 + "," + item2 + "," + item3 + "," + item4); // places current items inside the myDB array as a new array

if (myDB[0][3]=="4") {
alert(myDB[0][3]);
}
</script>

I've picked up that the array is read as a string. What I want the script to do is treat each comma separated value as a value and to be able to directly select the value (i.e. myDB[0][0]) of the array within the array.

coothead
10-31-2010, 11:13 AM
Hi there sniperman,

I do not really understand what it is that you are trying to achieve. :confused:
But then, I am well past my "Sell By" date. :D

Nevertheless, my modification to your script appears to provide your required result....


<script type="text/javascript">

myDB=new Array();

item1='1';
item2='2';
item3='3';
item4='4';

myDB.push(item1+item2+item3+item4);

if(myDB[0][3]=='4') {

alert(myDB[0][3]);
}

</script>


but not in IE8, though :)
coothead

sniperman
10-31-2010, 11:49 AM
Hi coothead

I am trying to create a multi-dimensional array and one that is indexed.

The basic function this script needs to replicate is the ability to look in multi-dimensional arrays for a value.


<script type="text/javascript">

myDB=new Array();

item1='1';
item2='2';
item3='3';
item4='four';

myDB.push(item1+item2+item3+item4);

alert(myDB[0][3]);

if(myDB[0][3]=='four') {

alert(myDB[0][3]);
}

</script>

Your code works but when you throw a value with more than one character you'll see the code above returns "f" instead of "four" because the array within the array is not indexed.

The line below seems to work


myDB = new Array(new Array(item1, item2, item3, item4)) ;

However, I am working with the push method to populate an array's node with another array, which does not seem to work on the fly.

coothead
10-31-2010, 12:55 PM
Hi there sniperman,

it appears that "item" is a reserved word in IE. :eek:
http://www.springenwerk.com/2009/01/is-reserved-word-in-internet-explorer-6.html

Would this be acceptable to your purpose...


<script type="text/javascript">

myDB=[];
myItem=[];

myDB[0]=myItem;

myItem[0]='1';
myItem[1]='2';
myItem[2]='3';
myItem[3]='four';

if(myDB[0][3]=='four') {

alert(myDB[0][3]);

}
</script>

coothead

sniperman
10-31-2010, 01:27 PM
Yeah that's awesome coothead exactly the result I was looking for.

I also went here (http://www.hunlock.com/blogs/Mastering_Javascript_Arrays) to read up on arrays.


<script type="text/javascript">

myDB=[];
myItem=[];

myDB.push(myItem);


myItem[0]='1';
myItem[1]='2';
myItem[2]='3';
myItem[3]='four';

if(myDB[0][3]=='four') {

alert(myDB[0][3]);

}
</script>

I've modified the script a little bit so that the items can be dynamically inserted into the array's index, which is how the script rolls :)

That optimization should remove a couple of lines of old code and make the script perform better.

ps. I had no idea that item was a reserved word thanks for that. I'm working with Firefox so not sure whether the same problem exists.

Sniper!

coothead
10-31-2010, 03:58 PM
I am pleased to have helped in some small way. ;)

sniperman
11-26-2010, 12:00 PM
Ok, so I'm back to punish myself with multidimensional arrays. I've taken the model above and adapted it but it needs some work to deliver the desired result.

I need to accomplish this:


myDB=[];

column 1: text
column 2: value

x x
[0] |_|_| [0]
[1] |_|_| [0]
[2] |_|_| [0]
[3] |_|_| [0]
[4] |_|_| [0]

code is not to scale :)

an example would be 5 objects with 1 attribute each i.e.


myDB=[];

var rand=Math.floor(Math.random()*20;
myDB[0] = ["random1", rand];
var rand=Math.floor(Math.random()*20;
myDB[0].push("random1", rand)

I want them to be invoked something like this


alert(myDB[0][0])

should equal "random1" and "random number"