472,332 Members | 1,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

beginner: unique values in an array

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
8 18474


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
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

"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

<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
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
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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jason | last post by:
NEWBIE - Hello. I've been able to do the following in other languages (perl,jscript,etc.) but am a asp.net newbie and need some help. I have...
1
by: Joep | last post by:
Hi, In VB.net I have an array which contains different values. For example: array(0) = 2 array(1) = 2 array(2) = 2 array(3) = 3 array(4) = 3...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an...
1
by: Fred | last post by:
Hi all, I have a DataTable that is being filled with data from an xml file. Now I need to get an array of strings from that DataTable that...
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show...
11
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation a table with columns and rows
0
by: BlackMustard | last post by:
how can i ensure that an array consists of only unique values? in one of my subroutines i generate an array with a list of search strings, and i...
1
by: dirt | last post by:
Hi I want to sort an array by non unique Values. i have array named xo for example with values: xo="n"; xo="n"; xo="n"; xo="n"; xo="u";
5
by: macca | last post by:
Hi, I'm doing an two ldap_search queries and I need to combine the two results into one single array containing all the results from each but...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.