[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

JS Array problem

Mel Smith

10/20/2014 9:12:00 PM

Problem:

I have seveal undefined objects/vrbls (?) in an array. I wish each of
these objects to
acquire a new value in a loop thru the array and
my brain has gone dead again :(

// Assume :

var a,b,c,d ;

var abcd = [a,b,c,d] ;

var somestr = "" ;

for (var i= 0; i<abcd.length; i++) {
somestr = i.toString() ;
// I wish to place the current value of somestr in each
// of the named but undefined objects/vrbls that are carried in the abcd
array

// e.g.,
abcd[i] = somestr ; // but this, of course, ruins everything :(

// what is the correct statement please ?
}

Result I wish here is:

// a has value '0'
// b has value '1'
// c has value '2'
// d has value '3'


Thanks for (another) kick-in-the-butt.

-Mel Smith



20 Answers

Denis McMahon

10/20/2014 10:26:00 PM

0

On Mon, 20 Oct 2014 15:11:45 -0600, Mel Smith wrote:

> var abcd = [a,b,c,d] ;

This creates an array that contains the values of the 4 variables a, b, c
and d. It does not create a connection between the array members and the
original variables such that editing the array values will change the
variables.

Why do you need the four values to be addressable as a,b,c,d as well as
abcd[n]?

--
Denis McMahon, denismfmcmahon@gmail.com

danca

10/21/2014 7:08:00 AM

0

Il 20/10/2014 23:11, Mel Smith ha scritto:
> Problem:
>
> I have seveal undefined objects/vrbls (?) in an array. I wish each of
> these objects to
> acquire a new value in a loop thru the array and
> my brain has gone dead again :(
>
> // Assume :
>
> var a,b,c,d ;
>
> var abcd = [a,b,c,d] ;
>
> var somestr = "" ;
>
> for (var i= 0; i<abcd.length; i++) {
> somestr = i.toString() ;
> // I wish to place the current value of somestr in each
> // of the named but undefined objects/vrbls that are carried in the abcd
> array
>
> // e.g.,
> abcd[i] = somestr ; // but this, of course, ruins everything :(
>
> // what is the correct statement please ?
> }
>
> Result I wish here is:
>
> // a has value '0'
> // b has value '1'
> // c has value '2'
> // d has value '3'
>
>
> Thanks for (another) kick-in-the-butt.
>
> -Mel Smith
>
>
>
Maybe something like this?

var a,b,c,d ;
var abcd = ["a","b","c","d"] ;
for (var i= 0; i<abcd.length; i++) {
eval(abcd[i]+"="+i);
}
Dan

Une Bévue

10/21/2014 7:42:00 AM

0

Le 21/10/14 09:08, danca a écrit :
> var abcd = ["a","b","c","d"] ;

or :
var abcd = 'a b c d'.split(' ');

Evertjan.

10/21/2014 7:48:00 AM

0

=?ISO-8859-15?Q?Une_B=E9vue?= <unbewusst.sein@fai.invalid> wrote on 21 okt
2014 in comp.lang.javascript:

> Le 21/10/14 09:08, danca a écrit :
>> var abcd = ["a","b","c","d"] ;
>
> or :
> var abcd = 'a b c d'.split(' ');

Skip the whitespace noise:

var abcd = 'abcd'.split('');

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Une Bévue

10/21/2014 7:52:00 AM

0

Le 21/10/14 09:47, Evertjan. a écrit :
> Skip the whitespace noise:
>
> var abcd = 'abcd'.split('');

fine !

Une Bévue

10/21/2014 7:52:00 AM

0

Le 21/10/14 09:47, Evertjan. a écrit :
> Skip the whitespace noise:
>
> var abcd = 'abcd'.split('');

I'm using for words then, i need a separator.

Evertjan.

10/21/2014 7:54:00 AM

0

Une Bévue <unbewusst.sein@fai.invalid> wrote on 21 okt 2014 in
comp.lang.javascript:

> Le 21/10/14 09:47, Evertjan. a écrit :
>> Skip the whitespace noise:
>>
>> var abcd = 'abcd'.split('');
>
> fine !

or more generic:

var abcd = [];
for (i=97;i<101;i++)
abcd.push(String.fromCharCode(i));



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Evertjan.

10/21/2014 8:36:00 AM

0

Une Bévue <unbewusst.sein@fai.invalid> wrote on 21 okt 2014 in
comp.lang.javascript:

> Le 21/10/14 09:47, Evertjan. a écrit :
>> Skip the whitespace noise:
>>
>> var abcd = 'abcd'.split('');
>
> I'm using for words then, i need a separator.

Not needed if all words have the same length:

var months = 'janfebmaraprmayjunetc'.match(/.{3}/g);

==================================

having different length words yes, a separator is usefull:

var months = 'january/february/march/april/may/june/etc'.split('/');

var months = 'january/february/march/april/may/june/etc'.match(/[^\/]+/g);

==================================

but even then not always necessary:

var months = 'JanuaryFebruaryMarchAprilMayJuneEtc'.match(/[A-Z][a-z]+/g);


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Christoph M. Becker

10/21/2014 11:12:00 AM

0

Mel Smith wrote:

> var a,b,c,d ;
>
> var abcd = [a,b,c,d] ;
>
> var somestr = "" ;
>
> for (var i= 0; i<abcd.length; i++) {
> somestr = i.toString() ;
> // I wish to place the current value of somestr in each
> // of the named but undefined objects/vrbls that are carried in the abcd
> array
>
> // e.g.,
> abcd[i] = somestr ; // but this, of course, ruins everything :(
>
> // what is the correct statement please ?

As you only want *undefined* values to be replaced, you have to check
for that:

if (typeof(abcd[i]) == "undefined") {
abcd[i] = somestr;
}

> }

--
Christoph M. Becker

Mel Smith

10/21/2014 2:29:00 PM

0

I said:

> // Assume :
>
> var a,b,c,d ;
>
> var abcd = [a,b,c,d] ;

BEFORE I read any of the many replies to my post, I want to say that I
now realize my question/problem was based on not realizing that *values* not
*references/pointers* were placed in my abcd vrbl. I *should* (I think)
have inserted referenes to the vars a,b,c,d in the abcd array *not* the
undefined value of those vars.

My 'insights' always seem to occur *after* I post my problem :((

Now I'm going to read the rest of the many posts.


-Mel