473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array Help

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
4 1275
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
<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
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
>>><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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: point | last post by:
Hi there.. I have the folowing array => property = hexonet => property = 2003-12-01 18:46:20.0 => property = hexonet => property = 2003-12-02 02:59:15.0 => property = hexonet =>...
2
by: Antti Nummiaho | last post by:
Consider the following javascript: var temp = new Array(new Array(0)) document.writeln(temp) temp = new Array(new Array(0,1)) document.writeln(temp) One would assume that it would print "0...
8
by: Gactimus | last post by:
I made the program below. It outputs the smallest number in the array. What I would like to know is how do I output the array location. I am at a loss. For example, since the smallest number in...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
3
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue = ary(3, 1) 'xValue = C...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
5
by: fluk | last post by:
Hi Guys, I hope someone can help me with this, because i'm getting crazy to find a good way to do that! This is what I got by querying a db. $arr1 = array("site", "description", "area1" ,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.