473,473 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

test for uniqueness problem

Hi Guys and Gals,

Please can u help me with this little problem.

I am collecting a set of values from a web form and putting them into a
JS array object.
I now want to check/ensure that there or no repeated values in this array.
I can think of many ways to do this using one or many loops but was just
wondering if there was
any other clever way of doing this using string functions or the RegExp
object perhaps.

Needless to say I am a JS virgin so any help or advice would be much
appreciated.

Regards.
Naran Hirani

Jul 20 '05 #1
6 3884
> I am collecting a set of values from a web form and putting them into a
JS array object.
I now want to check/ensure that there or no repeated values in this array.
I can think of many ways to do this using one or many loops but was just
wondering if there was
any other clever way of doing this using string functions or the RegExp
object perhaps.


Don't use an array, use an object. Use the values as the keys of the object.
Then use for..in to extract the keys.

var o = {};
... some process that produces values ...
o[value] = true;
}
for (value in o) {
if (value === true) {
... value is unique ...
}
}

http://www.crockford.com/#javascript
Jul 20 '05 #2
Naran Hirani <N.******@hgmp.mrc.ac.uk> writes:
I am collecting a set of values from a web form and putting them into
a JS array object.

I now want to check/ensure that there or no repeated values in this
array. I can think of many ways to do this using one or many loops
but was just wondering if there was

any other clever way of doing this using string functions or the
RegExp object perhaps.
That is sadly a problem that is not solvable by regular expressions [1].
Needless to say I am a JS virgin so any help or advice would be much
appreciated.


What you can do is to create the elements as properties of an object,
and then check whether you try to create the same property twice.

This function should check an array for duplicate entries (it expects
the entries to be strings, otherwise they are converted to strings
before comparing).

function containsDoubles(array) {
var object = {};
for (var i in array) {
if (object[array[i]]) {
return true;
}
object[array[i]] = true;
}
return false;
}

You could also make this check while adding the elements to the array,
but I doubt the change in efficiency is worth it.

/L
[1] Perl's regular expressions might work, since they extend their
RegExps with features that are not regular, but it will not be efficient.
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Thanks Douglas, and Lasse for your replies and code fragments.
I was all set to tackle this problem using arrays, but after your
warning and suggestions
I will implement something along the lines you guys are suggesting.

Regards,
Naran.
Douglas Crockford wrote:
I am collecting a set of values from a web form and putting them into a
JS array object.
I now want to check/ensure that there or no repeated values in this array.
I can think of many ways to do this using one or many loops but was just
wondering if there was
any other clever way of doing this using string functions or the RegExp
object perhaps.


Don't use an array, use an object. Use the values as the keys of the object.
Then use for..in to extract the keys.

var o = {};
... some process that produces values ...
o[value] = true;
}
for (value in o) {
if (value === true) {
... value is unique ...
}
}

http://www.crockford.com/#javascript


Jul 20 '05 #4
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:d6**********@hotpop.com...
<snip>
function containsDoubles(array) {
var object = {};
for (var i in array) {
if (object[array[i]]) {
return true;
}
object[array[i]] = true;
}
return false;
}


I was recently bitten while trying this. Somehow the string
'constructor' found its way into the data and of course
object.constructor is always true. I ended up having to explicitly
prefix all of the data strings with 'Q_' to safely be able to use them
as Object property names without risking conflicts with existing
JavaScript Object properties.

Richard.
Jul 20 '05 #5
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:
I was recently bitten while trying this. Somehow the string
'constructor' found its way into the data and of course
object.constructor is always true. I ended up having to explicitly
prefix all of the data strings with 'Q_' to safely be able to use them
as Object property names without risking conflicts with existing
JavaScript Object properties.


Good point! The apparently empty object ... isn't.
You could also prefix with " " (space), but "Q_" is probably safe enough.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:u1**********@hotpop.com...
<snip>
You could also prefix with " " (space), but "Q_" is
probably safe enough.


That is a good suggestion, I was thinking in terms of legal JavaScript
identifiers and looking for an identifier prefix that would be
improbable. A space character is not only improbable but actually
impossible in dot notation, and in the context of a square bracket
reference its legality as an identifier is not significant.

It might make the code less easy to comprehend, being superficially
mistakable for - ""+stringVar; - as a method to force type conversion to
a string, but an appropriate comment should sort that out.

Richard.
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Puvendran Selvaratnam | last post by:
Hi, First of all my apologies if you have seen this mail already but I am re-sending as there were some initial problems. This query is related to defining indexes to be unique or not and...
1
by: Karsten Hilbert | last post by:
There recently was a discussion on how to enforce uniqueness on a row for a given condition, say allow many address rows for a person where active = false but only one where active = true. I...
6
by: Andreas | last post by:
Hello list, what about uniqueness of inherited primary keys ? eg you have : create table objects ( id int4, date_created timestamp(0), primary key (id)
2
by: Dirk Declercq | last post by:
Hi, Is it possible in Xml to enfore the uniqueness of an element based on his attribute value. Say I have this schema : <?xml version="1.0" encoding="UTF-8"?> <xs:schema...
1
by: Mr. Almenares | last post by:
Hello: I’m trying to do a schema with recurrent structure for a Book like a Node can have many Nodes inside or One leave. So, the leaves have an attribute that is Identifier. My goal is define...
1
by: MDS | last post by:
All, I am endeavouring to implement an "Also in the area" feature to an Access 97 DB. Within the table, there are two columns drawn from the same domain - let's call them Place A and Place B....
0
by: RICHARD BROMBERG | last post by:
I am writing an Access 2000 program for scoring an athletic tournament. I have a Members table (tblMembers) that contains Member information like Name, Address, Member Number. Member Number is...
0
by: Matthew Wieder | last post by:
I have the following requirement: A form with 2 ListViews. lvSource which contains a list of items that the user can select from and lvTarget where the selected item(s) end up. Items should...
5
by: Alan Little | last post by:
I have affiliates submitting batches of anywhere from 10 to several hundred orders. Each order in the batch must include an order ID, originated by the affiliate, which must be unique across all...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.