By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,714 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,714 IT Pros & Developers. It's quick & easy.

Array Help

P: n/a
Hi all,

If I have an array with multiple entries that are the same. How can I split
them up into separate arrays by this criteria?

var myArry =
("cars","cars","cars","cars","sticks","sticks","st icks","trees","trees");

The desired result would separate this array into 3 arrays. The result being
a multidimensional array would be fine.

myNewArry =
[["cars","cars","cars","cars"],["sticks","sticks","sticks"],["trees","trees"]];

David
Jun 15 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jun 14, 5:08 pm, "David" <n...@none.netwrote:
Hi all,

If I have an array with multiple entries that are the same. How can I split
them up into separate arrays by this criteria?

var myArry =
("cars","cars","cars","cars","sticks","sticks","st icks","trees","trees");

The desired result would separate this array into 3 arrays. The result being
a multidimensional array would be fine.

myNewArry =
[["cars","cars","cars","cars"],["sticks","sticks","sticks"],["trees","trees"]];

David
var
// s = myArry.join(","), // or ...
s = "cars,cars,cars,cars,sticks,sticks,sticks,trees,tr ees",
res = []
;
s.replace( /(\w+)(,\1)*/g,
function ( a ) {
res.push( a.split( "," ) );
} );
alert( res.join( "\n" ) );

--
../rh

Jun 15 '07 #2

P: n/a
<ro********@gmail.comwrote in message:
var
// s = myArry.join(","), // or ...
s = "cars,cars,cars,cars,sticks,sticks,sticks,trees,tr ees",
res = []
;
s.replace( /(\w+)(,\1)*/g,
function ( a ) {
res.push( a.split( "," ) );
} );
alert( res.join( "\n" ) );
------------------------------------------

Ron,

That's brilliant. Thank you. You know as simple as this is I don't
understand it, but am trying.

Delimit the array entries with a comma.
replace any (set of characters) (comma and a 1)

Why is there a 1 ( a numeral ) in this? The array contains no numerals.

Then replace that with what the function(a){} returns. Where is a defined?

It works and is very elegant but, lol, I would love to know why. You should
see the mess I had :-)

David
Jun 15 '07 #3

P: n/a
On Jun 14, 10:05 pm, "David" <n...@none.netwrote:
>><ron.h.h...@gmail.comwrote in message:

var
// s = myArry.join(","), // or ...
s = "cars,cars,cars,cars,sticks,sticks,sticks,trees,tr ees",
res = []
;
s.replace( /(\w+)(,\1)*/g,
function ( a ) {
res.push( a.split( "," ) );
} );
alert( res.join( "\n" ) );
------------------------------------------

Ron,

That's brilliant. Thank you. You know as simple as this is I don't
understand it, but am trying.

Delimit the array entries with a comma.
replace any (set of characters) (comma and a 1)

Why is there a 1 ( a numeral ) in this? The array contains no numerals.
The \(digit) is related to the [now deprecated] RegExp.$(digit). \
(digit) makes reference to the captured text match in the (1st \1, 2nd
\2, ...) set of capturing parentheses as specified by the digit. The
difference is that \(digit) can be used during the matching process to
specify text that is to be matched.
Then replace that with what the function(a){} returns. Where is a defined?
As given, the function returns a value that is undefined. But, because
the generated replacement is not assigned, it's not relevant and is
discarded. So the .replace is just a mechanism to provide the function
calls with the appropriate values as the matching operations progress.

"a" is provided with its value by the caller, the regular expression
matching process, and provides the text string that is currently
matched on each successive call.
It works and is very elegant but, lol, I would love to know why. You should
see the mess I had :-)
Some other time, perhaps. ;-)

Note there are other ways to get the desired result in Javascript with
a looping construct, that should be reasonably clean and short.

--
../rh
Jun 15 '07 #4

P: n/a
>>><ron.h.h...@gmail.comwrote in message:
>Ron,

That's brilliant. Thank you. You know as simple as this is I don't
understand it, but am trying.

Delimit the array entries with a comma.
replace any (set of characters) (comma and a 1)

Why is there a 1 ( a numeral ) in this? The array contains no numerals.

The \(digit) is related to the [now deprecated] RegExp.$(digit). \
(digit) makes reference to the captured text match in the (1st \1, 2nd
\2, ...) set of capturing parentheses as specified by the digit. The
difference is that \(digit) can be used during the matching process to
specify text that is to be matched.
>Then replace that with what the function(a){} returns. Where is a
defined?

As given, the function returns a value that is undefined. But, because
the generated replacement is not assigned, it's not relevant and is
discarded. So the .replace is just a mechanism to provide the function
calls with the appropriate values as the matching operations progress.

"a" is provided with its value by the caller, the regular expression
matching process, and provides the text string that is currently
matched on each successive call.
>It works and is very elegant but, lol, I would love to know why. You
should
see the mess I had :-)

Some other time, perhaps. ;-)

Note there are other ways to get the desired result in Javascript with
a looping construct, that should be reasonably clean and short.

I think I'll stay with this method, as it facinates me. Thanks for the
explanation.

David
Jun 15 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.