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

test for uniqueness problem

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
"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.