Array Help

D

David

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","sticks","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
 
R

ron.h.hall

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","sticks","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,trees",
res = []
;
s.replace( /(\w+)(,\1)*/g,
function ( a ) {
res.push( a.split( "," ) );
} );
alert( res.join( "\n" ) );
 
D

David

var
// s = myArry.join(","), // or ...
s = "cars,cars,cars,cars,sticks,sticks,sticks,trees,trees",
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
 
R

ron.h.hall

var
// s = myArry.join(","), // or ...
s = "cars,cars,cars,cars,sticks,sticks,sticks,trees,trees",
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.
 
D

David

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top