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

beginner: unique values in an array

P: n/a
hello,

i have an array and i don't know the content of it, but i want only unique
values.

in php there is a function to do this, but how must i do this in javascript?

i have tried a lot and it is driving me nuts
thanks

Jan 11 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a


nescio wrote:
i have an array and i don't know the content of it, but i want only unique
values.


When do you want unique values, when looping through the array to read
out values, when putting new values into the array, or when exactly?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 11 '06 #2

P: n/a
nescio wrote on 11 jan 2006 in comp.lang.javascript:
i have an array and i don't know the content of it, but i want only
unique values.

in php there is a function to do this, but how must i do this in
javascript?

i have tried a lot and it is driving me nuts


Make a second array containing the content as the enumerated pointer and
the number as the content.
<script type="text/JavaScript">

arr1 = new Array()
arr2 = new Array()

arr1[0] = "hello"
arr1[1] = "world"
arr1[2] = "hello"

for (c in arr1)
arr2[arr1[c]] = c

for (c in arr2)
document.write(arr2[c] + ' = ' + c + '<br>');

alert(arr1.length)

alert(arr2.length)

</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 11 '06 #3

P: n/a

"Martin Honnen" <ma*******@yahoo.de> schreef in bericht
news:43***********************@newsread4.arcor-online.net...


nescio wrote:
i have an array and i don't know the content of it, but i want only
unique values.


When do you want unique values, when looping through the array to read out
values, when putting new values into the array, or when exactly?

--

Martin Honnen
http://JavaScript.FAQTs.com/

i have a form with 4 listboxes;
every listbox has the same values;

people must select in every listbox a different value;
so i want to store the values they choose in an array and then see if all
the
elements of the array are unique, if so, the form is sent;
if not, they have to change a listbox;

nesico



Jan 11 '06 #4

P: n/a

<script type="text/JavaScript">

arr1 = new Array()
arr2 = new Array()

arr1[0] = "hello"
arr1[1] = "world"
arr1[2] = "hello"

for (c in arr1)
arr2[arr1[c]] = c

for (c in arr2)
document.write(arr2[c] + ' = ' + c + '<br>');

alert(arr1.length)

alert(arr2.length)

</script>


this is a great help,

thanks
Jan 11 '06 #5

P: n/a
Just use an if statement such as:

var arr = [];

arr[0] = "A";
arr[1] = "A";
arr[2] = "A";
arr[3] = "A";

if (arr[0] == arr[1] && arr[0] == arr[2] && arr[0]== arr[3]){
alert("Please make sure that all boxes have diff values");
}
else{
document.formName.submit();
}

Jan 11 '06 #6

P: n/a
Matt wrote:
Just use an if statement such as:

var arr = [];

arr[0] = "A";
arr[1] = "A";
arr[2] = "A";
arr[3] = "A";
var arr = ["A", "A", "A", "A"];
if (arr[0] == arr[1] && arr[0] == arr[2] && arr[0]== arr[3]){
alert("Please make sure that all boxes have diff values");


The cases arr[1] == arr[2], arr[1] == arr[3] and arr[2] == arr[3]
are not covered by that.

And think about where this approach would get you if there were
five or more elements in the array. It is utter nonsense, indeed.

Instead, you can use Object objects:

Array.prototype.uniqueValues = function()
{
var result = true, o = {};

function hadThisBefore(v, i, a)
{
/*
* there is no known property inherited from
* Object to have a "___" prefix for its name
*/
var p = "___" + v;

if (!o[p])
{
o[p] = true;
return false;
}
else
{
return true;
}
}

// Supports JavaScript 1.6 as implemented
// in Gecko rv:1.8, including Firefox 1.5
if (typeof this.some == "function")
{
result = !this.some(hadThisBefore);
}
else
{
for (var i = this.length; i--;)
{
if (hadThisBefore(this[i], i, this))
{
result = false;
break;
}
}
}

return result;
}

var a = ["A", "A", "A", "A"];
if (!a.uniqueValues())
{
window.alert("Please make sure that all boxes have different values.");
}
PointedEars
Jan 11 '06 #7

P: n/a
JRS: In article <43***********************@news.inter.NL.net>, dated
Wed, 11 Jan 2006 15:55:22 local, seen in news:comp.lang.javascript,
nescio <ne****@nescio.nl> posted :
so i want to store the values they choose in an array and then see if all
the
elements of the array are unique,


To determine whether the elements of an array of strings are unique, in
a reasonably efficient manner, sort the array (.sort() method) then scan
once through the array to see if any element is the same as the previous
one.

If they are not strings, or are to be considered as non-strings, use
instead .sort(Fn) where Fn is a suitable comparison function. For
example, if the elements are strings representing numbers, and "1e3",
"1000", etc., are to be considered equal, function Fn(a, b) { return
a-b } should IIRC do it.

If they are strings guaranteed to be suitable as Object property names,
or easily converted to such (e.g. by prepending a letter) then you can
declare an Object and give it the corresponding property after first
checking that the property does not yet exist.

If they are strings guaranteed "hashable" to distinct non-negative
integers, then you can do much the same with an Array.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 11 '06 #8

P: n/a
<snip>
i have a form with 4 listboxes;
every listbox has the same values;

people must select in every listbox a different value;
so i want to store the values they choose in an array and then see if all
the
elements of the array are unique, if so, the form is sent;
if not, they have to change a listbox;

nesico


Do the validation as they fill out the form.

As they make each selection, check to see if that selection has already been
chosen. If it has, alert them and require them to make another selection.

You could also disable/remove the selections, that have already been chosen,
as the fill out the form.

Another option would be to have all values present in the first select's
option list then populate the following selects with the remaining values,
etc.

Check out http://www.mattkruse.com/javascript/dynamicoptionlist/

Rich
Jan 13 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.