473,573 Members | 3,208 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18585


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.javas cript:
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.leng th)

alert(arr2.leng th)

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

"Martin Honnen" <ma*******@yaho o.de> schreef in bericht
news:43******** *************** @newsread4.arco r-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.leng th)

alert(arr2.leng th)

</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.formNa me.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(hadT hisBefore);
}
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.uniqueValue s())
{
window.alert("P lease make sure that all boxes have different values.");
}
PointedEars
Jan 11 '06 #7
JRS: In article <43************ ***********@new s.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
1283
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 this textbox that has a bunch of data in it. At the beginning of almost every line I have some names followed by a ":" Many (about 3/4) of the names will be duplicates. I'd like to produce a unique...
1
7180
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 array(5) = 6 array(6) = 6
5
28331
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 easy way to do this? I have tried looping through each element but I am having issues using redim to adjust the new array. Any help or example code would be greatly appreciated. thanks!
1
2000
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 returns all the unique values in a certain column (column name would be 'project' and will at max contain 30 different projects). What would be the best way to do this? Using a RowFilter and then pass...
9
5666
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 it to work.) Public Function Unique(ByVal List() As String) As String() ' Returns the unique values of in array, in an array. Dim Temp As New System.Collections.Specialized.StringCollection()
11
3928
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
1065
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 want to show all search strings that didn't generate any hit. so when a search for a file with the specified name fails, the name is stored in an array. however, in most cases each string is used...
1
1679
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
3594
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 removing duplicates. I have tried built in php functions such as array_merge (which gives me duplicates) and array_unique which does not work either.
0
7977
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8165
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5252
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2163
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1256
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
984
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.