471,122 Members | 1,148 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 3735
> 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Puvendran Selvaratnam | last post: by
1 post views Thread by Karsten Hilbert | last post: by
2 posts views Thread by Dirk Declercq | last post: by
1 post views Thread by Mr. Almenares | last post: by
1 post views Thread by MDS | last post: by
reply views Thread by RICHARD BROMBERG | last post: by
reply views Thread by Matthew Wieder | last post: by
5 posts views Thread by Alan Little | last post: by

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.