Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #25942 > unrolled thread
| Started by | "Mel Smith" <syntel@cox.net> |
|---|---|
| First post | 2014-10-20 15:11 -0600 |
| Last post | 2014-10-28 10:08 +0100 |
| Articles | 20 on this page of 21 — 9 participants |
Back to article view | Back to comp.lang.javascript
JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-20 15:11 -0600
Re: JS Array problem Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-20 22:25 +0000
Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:33 -0600
Re: JS Array problem danca <cyberdanny@tiscalinet.it> - 2014-10-21 09:08 +0200
Re: JS Array problem Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 09:42 +0200
Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-21 09:47 +0200
Re: JS Array problem Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 09:51 +0200
Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-21 09:53 +0200
Re: JS Array problem Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 09:52 +0200
Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-21 10:36 +0200
Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:36 -0600
Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:35 -0600
Re: JS Array problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-21 19:02 +0200
Re: JS Array problem "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-10-21 13:12 +0200
Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:38 -0600
Re: JS Array problem "Mel Smith" <syntel@cox.net> - 2014-10-21 08:29 -0600
My 'actual' code "Mel Smith" <syntel@cox.net> - 2014-10-21 11:20 -0600
Re: My 'actual' code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-10-21 19:52 +0100
Re: My 'actual' code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-21 21:23 +0200
Re: JS Array problem Violet Deng <dengviolet@gmail.com> - 2014-10-28 02:03 -0700
Re: JS Array problem "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-10-28 10:08 +0100
Page 1 of 2 [1] 2 Next page →
| From | "Mel Smith" <syntel@cox.net> |
|---|---|
| Date | 2014-10-20 15:11 -0600 |
| Subject | JS Array problem |
| Message-ID | <calc4vFrm5iU1@mid.individual.net> |
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
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-10-20 22:25 +0000 |
| Message-ID | <m2424p$mdn$1@dont-email.me> |
| In reply to | #25942 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Mel Smith" <syntel@cox.net> |
|---|---|
| Date | 2014-10-21 08:33 -0600 |
| Message-ID | <can95rFbqnlU1@mid.individual.net> |
| In reply to | #25943 |
"Denis McMahon" <denismfmcmahon@gmail.com> wrote in message news:m2424p$mdn$1@dont-email.me... > 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]? > In my 'real' app, there are consistency probs where those vrbls and many others are built and returned to the server under those vrbl names a few lines below that 'collection area'. Thanks, and please not my reply to myself -Mel
[toc] | [prev] | [next] | [standalone]
| From | danca <cyberdanny@tiscalinet.it> |
|---|---|
| Date | 2014-10-21 09:08 +0200 |
| Message-ID | <m250og$r1l$1@dont-email.me> |
| In reply to | #25942 |
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
[toc] | [prev] | [next] | [standalone]
| From | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| Date | 2014-10-21 09:42 +0200 |
| Message-ID | <m252oq$gcb$1@shakotay.alphanet.ch> |
| In reply to | #25946 |
Le 21/10/14 09:08, danca a écrit :
> var abcd = ["a","b","c","d"] ;
or :
var abcd = 'a b c d'.split(' ');
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2014-10-21 09:47 +0200 |
| Message-ID | <XnsA3CD639F951A9eejj99@194.109.133.133> |
| In reply to | #25947 |
=?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)
[toc] | [prev] | [next] | [standalone]
| From | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| Date | 2014-10-21 09:51 +0200 |
| Message-ID | <m253ac$h9h$2@shakotay.alphanet.ch> |
| In reply to | #25948 |
Le 21/10/14 09:47, Evertjan. a écrit :
> Skip the whitespace noise:
>
> var abcd = 'abcd'.split('');
fine !
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2014-10-21 09:53 +0200 |
| Message-ID | <XnsA3CD64ABB8414eejj99@194.109.133.133> |
| In reply to | #25949 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| Date | 2014-10-21 09:52 +0200 |
| Message-ID | <m253bm$h9h$3@shakotay.alphanet.ch> |
| In reply to | #25948 |
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.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2014-10-21 10:36 +0200 |
| Message-ID | <XnsA3CD6BE2F1EDBeejj99@194.109.133.133> |
| In reply to | #25950 |
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)
[toc] | [prev] | [next] | [standalone]
| From | "Mel Smith" <syntel@cox.net> |
|---|---|
| Date | 2014-10-21 08:36 -0600 |
| Message-ID | <can9c8Fbs8hU1@mid.individual.net> |
| In reply to | #25947 |
"Une Bévue" <unbewusst.sein@fai.invalid> wrote in message
news:m252oq$gcb$1@shakotay.alphanet.ch...
> Le 21/10/14 09:08, danca a écrit :
>> var abcd = ["a","b","c","d"] ;
>
> or :
> var abcd = 'a b c d'.split(' ');
????
Don't understand
-Mel
[toc] | [prev] | [next] | [standalone]
| From | "Mel Smith" <syntel@cox.net> |
|---|---|
| Date | 2014-10-21 08:35 -0600 |
| Message-ID | <can99nFbrnaU1@mid.individual.net> |
| In reply to | #25946 |
"danca" <cyberdanny@tiscalinet.it> wrote in message
news:m250og$r1l$1@dont-email.me...
> Il 20/10/2014 23:11, Mel Smith ha scritto:
>>
>>
> 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);
> }
Hi Dan:
Yes, That's what I was looking for.
Thank you !
-Mel
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-10-21 19:02 +0200 |
| Message-ID | <1819918.Z1tkqcoRQN@PointedEars.de> |
| In reply to | #25960 |
Mel Smith wrote:
> "danca" <cyberdanny@tiscalinet.it> wrote […]:
>> Il 20/10/2014 23:11, Mel Smith ha scritto:
>> 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);
>> }
>
> […]
> Yes, That's what I was looking for.
Hardly, as it is harder to debug and more error-prone. It will not work in
strict mode (the scope chain of eval code is empty then), which you should
be using in order to avoid common programming errors.
Also,
for (var i = 0, len = abcd.length; i < len; ++i)
is more efficient when “abcd.length” does not change;
for (var i = abcd.length; i--;)
is even more efficient if the array is not sparse and order does not matter.
If the array-like object is sparse, the most efficient way, where order
matters and non-index own properties are not expected, is probably
var indexes = Object.keys(abcd);
for (var i = 0, len = indexes.length; i < len; ++i)
{
var index = indexes[i];
abcd[index] = …;
}
(Array indexes are names of own enumerable properties. If non-index own
properties are expected, an additional test of the property name is
required.)
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | "Christoph M. Becker" <cmbecker69@arcor.de> |
|---|---|
| Date | 2014-10-21 13:12 +0200 |
| Message-ID | <m25f2l$hks$1@solani.org> |
| In reply to | #25942 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Mel Smith" <syntel@cox.net> |
|---|---|
| Date | 2014-10-21 08:38 -0600 |
| Message-ID | <can9g8FbtfoU1@mid.individual.net> |
| In reply to | #25956 |
Hi All:
Thank you for the responses.
I'm off and running again :))
-Mel
[toc] | [prev] | [next] | [standalone]
| From | "Mel Smith" <syntel@cox.net> |
|---|---|
| Date | 2014-10-21 08:29 -0600 |
| Message-ID | <can8usFboljU1@mid.individual.net> |
| In reply to | #25942 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Mel Smith" <syntel@cox.net> |
|---|---|
| Date | 2014-10-21 11:20 -0600 |
| Subject | My 'actual' code |
| Message-ID | <caniujFef23U1@mid.individual.net> |
| In reply to | #25958 |
Hi All:
FYI, below is the actual code I'm currently working on.
I've included Danca's Eval() code suggestions and
thought about Evertjan's improvement.
btw, it works great !
Also, I'll be examining Thomas Lahn's other suggestions this afternoon.
In my 'for' loops, I usually pre-calc the length instead of using .length in
the header). I didn't bother this time but will change it later.
Thanks to all
-Mel
<snipped about 60 lines about in this func>
//vars i,j are local vars from above of course
// the values I'm gathering are single-char values in a 5 row, 23 column
input table, where there are only 18 input elements in each row.
// As the user inputs single values, he is 'kicked' to the next element
(either verticall or horizontally), and the read-only fields in tge row are
updated
// there will be five (for now) strings submitted to the server - one string
from each row
**************************
// now have to code the table for all internal values
var hscells,ptcells,ftcells,dlcells,rlcells ;
var hsstr,ptstr,ftstr,dlstr,rlstr ;
hsstr = ptstr = ftstr = dlstr = rlstr = "" ;
hscells = getobj("ihsrow").cells ;
ptcells = getobj("iptrow").cells ;
ftcells = getobj("iftrow").cells ;
dlcells = getobj("idlrow").cells ;
rlcells = getobj("irlrow").cells ;
var allrows = [hscells,ptcells,ftcells,dlcells,rlcells] ;
var allstrs = ["hsstr","ptstr","ftstr","dlstr","rlstr"] ; // could use
.split() maybe
var rowcells,rowstrval ;
var numcells = hscells.length ; // each row has same number of cells
var cellobj,cellid,cellval ;
for (j=0;j<allrows.length;j++) {
rowcells = allrows[j] ;
rowstrval = "" ;
for (i=1;i<numcells;i++) {
if (i === 10) continue ;
cellobj = rowcells[i].firstChild ;
cellid = cellobj.id ;
cellval = getobj(cellid).value ;
if (empty(cellval)) cellval = "X" ;
// do work on building a string here
if (i > 19) break ;
rowstrval += cellval +"^" ;
}
// select string reference and place composite value in it
// next is suggestion by Dan and later Evertjan from my JS ng on Oct
21/14
eval(allstrs[j] + '= "' + rowstrval+'"') ;
}
// testing code here
alert("hsstr="+hsstr) ;
alert("ptstr="+ptstr) ;
alert("ftstr="+ftstr) ;
alert("dlstr="+dlstr) ;
alert("rlstr="+rlstr) ;
// much more to be done but ...
return false ; // for now ...
// much more work to be done below
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2014-10-21 19:52 +0100 |
| Subject | Re: My 'actual' code |
| Message-ID | <87r3y16ysi.fsf@bsb.me.uk> |
| In reply to | #25966 |
"Mel Smith" <syntel@cox.net> writes:
> FYI, below is the actual code I'm currently working on.
As is so often the case, the actual code shows an altogether different
way to do things that avoids the problems in the question as asked!
<snip>
> <snipped about 60 lines about in this func>
> //vars i,j are local vars from above of course
> // the values I'm gathering are single-char values in a 5 row, 23 column
> input table, where there are only 18 input elements in each row.
> // As the user inputs single values, he is 'kicked' to the next element
> (either verticall or horizontally), and the read-only fields in tge row are
> updated
> // there will be five (for now) strings submitted to the server - one string
> from each row
> **************************
> // now have to code the table for all internal values
>
> var hscells,ptcells,ftcells,dlcells,rlcells ;
> var hsstr,ptstr,ftstr,dlstr,rlstr ;
>
> hsstr = ptstr = ftstr = dlstr = rlstr = "" ;
>
> hscells = getobj("ihsrow").cells ;
> ptcells = getobj("iptrow").cells ;
> ftcells = getobj("iftrow").cells ;
> dlcells = getobj("idlrow").cells ;
> rlcells = getobj("irlrow").cells ;
>
> var allrows = [hscells,ptcells,ftcells,dlcells,rlcells] ;
> var allstrs = ["hsstr","ptstr","ftstr","dlstr","rlstr"] ; // could use
> .split() maybe
It looks like you've sliced an object into different variables. I'd put
those objects back together again:
var all = [
{ cells: getobj("ihsrow").cells,
string: ""
},
{ cells: getobj("ipsrow").cells,
string: ""
},
{ cells: getobj("ifsrow").cells,
string: ""
},
{ cells: getobj("idsrow").cells,
string: ""
},
{ cells: getobj("irsrow").cells,
string: ""
}
];
though I'd be tempted to build these using a function -- only one letter
is needed to create the different objects.
Now when you iterate through the array, you have the cells the string
you are building linked together. No need to link them by a name and an
eval call (unless I've missed some key part from the code I cut -- I did
scan it in rather a hurry).
<snip code>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-10-21 21:23 +0200 |
| Subject | Re: My 'actual' code |
| Message-ID | <2072711.hqF0VMn4kS@PointedEars.de> |
| In reply to | #25969 |
Ben Bacarisse wrote:
> var all = [
> { cells: getobj("ihsrow").cells,
> string: ""
> },
> { cells: getobj("ipsrow").cells,
> string: ""
> },
> { cells: getobj("ifsrow").cells,
> string: ""
> },
> { cells: getobj("idsrow").cells,
> string: ""
> },
> { cells: getobj("irsrow").cells,
> string: ""
> }
> ];
This is asking for a DRY LART.
> though I'd be tempted to build these using a function -- only one letter
> is needed to create the different objects.
A function is not necessary:
var names = ["ihsrow", "ipsrow", "ifsrow", "idsrow", "irsrow"];
var all = [];
for (var i = 0, len = names.length; i < len; ++i)
{
all.push({cells: getobj(names[i]).cells, string: ""});
}
But (with an implementation of ECMAScript Ed. 5’s Array.prototype.map) with
regard to code length at least it can be optimized to
var all = names.map(function (name) {
return {cells: getobj(name).cells, string: ""};
});
and maybe even (longer, though)
var all = "hpfds".split("").map(function (letter) {
return {cells: getobj("i" + letter + "srow").cells, string: ""};
});
I suggest avoiding the identifier “all”, though: it is not self-explanatory,
and already used by a deprecated property of the object referred to by
“document”, which could interfere in an HTML context.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Violet Deng <dengviolet@gmail.com> |
|---|---|
| Date | 2014-10-28 02:03 -0700 |
| Message-ID | <850a8924-7b57-4a9d-b01f-fa4f32730a46@googlegroups.com> |
| In reply to | #25942 |
eval can solve this problem, but it's not recommended for poor security problem. you can try
for (var i= 0; i<abcd.length; i++) {
window[i] = "whatever you want";
}
this will be available if the a, b, c, d is belonged to window, you can also change window to any other namespace.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.javascript
csiph-web