/    Sign up×
Community /Pin to ProfileBookmark

Array of Arrays creation question

In the following script, the ‘choices’ array is defined correctly.
It also displays as expected with the showArray() function.

I was experimenting to see if I could dynamically create a similiar array of array (picks),
but I’m having difficulty with my syntax.

I highlighted the commands I believe are not correct in RED.

Does anyone have an idea how I can duplicate the ‘choices’ array into the ‘pick’ array.

[code]
<html>
<body>
<form id=”drop_list” onsubmit=”return false”>
<button onclick=”showArray(choices)”>Choice display</button>
<button onclick=”showPicks()”>Picks display</button>
</form>

<script type=”text/javascript”>
var choices = [ // an array OF arrays */
[ “AK”, “Alaska” ],
[ “AL”, “Alabama” ],
[ “AR”, “Arkansas” ],
[ “AZ”, “Arizona” ],
[ “CA”, “California” ],
[ “FL”, “Florida” ],
[ “GA”, “Georgia” ],
[ “TX”, “Texas” ],
[ “VT”, “Vermont” ] // Note: No comma after last entry
];

// want to create above array of arrays from strings below
var picks = [];
var arrStates = [
“AK:Alaska”, “AL:Alabama”, “AR:Arkansas”, “AZ:Arizona”,
“CA:California”, “FL:Florida”, “GA:Georgia”, “TX:Texas”, “VT:Vermont” ];

// following is NOT used at this time.
// var strStates = “AK:Alaska|AL:Alabama|AR:Arkansas|AZ:Arizona|CA:California|FL:Florida|GA:Georgia|TX:Texas|VT:Vermont”;

function showPicks() {
var tarr = [];
for (var i=0; i<arrStates.length; i++) {
tarr = arrStates[i].split(‘:’);
[COLOR=”#FF0000″] picks.push([tarr[0]],[tarr[1]]); // this does not appear to work as desired
// picks.push([tarr[0]]+’,’+[tarr[1]]); // neither does this
[/COLOR] }
showArray(picks);
}
function showArray(arr) {
var abb = sta = ”;
for (var i=0; i<arr.length; i++) { abb += arr[i][0]+’n’; }
for (var i=0; i<arr.length; i++) { sta += arr[i][1]+’n’; }
alert(arr.join(‘n’)+’nn’+abb+’nn’+sta);
}
</script>
</body>
</html>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Vince616Nov 22.2012 — Hi bud

You seem to have an extra set of square brackets in your push which is adding two elements to your array rather than the array if you change it to
[CODE]
picks.push([tarr[0],tarr[1]]);
[/CODE]


You could simplify a little more by doing
[CODE]
picks.push(arrStates[i].split(':'));
[/CODE]

This means that you could get rid of your temporary “tar” array.

Hope this helps

V
Copy linkTweet thisAlerts:
@JMRKERauthorNov 23.2012 — Thank you very much.

That was the syntax I was looking for.

I may have been close with my attempt,

but close only counts in horseshoes and handgranades.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...