test for uniqueness problem

N

Naran Hirani

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
 
D

Douglas Crockford

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
 
L

Lasse Reichstein Nielsen

Naran Hirani said:
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]) {
return true;
}
object[array] = 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.
 
N

Naran Hirani

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 said:
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
 
R

Richard Cornford

function containsDoubles(array) {
var object = {};
for (var i in array) {
if (object[array]) {
return true;
}
object[array] = 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.
 
L

Lasse Reichstein Nielsen

Richard Cornford said:
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
 
R

Richard Cornford

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.
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top