473,545 Members | 1,310 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 18580


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
7175
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
28321
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
1998
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
5659
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
3925
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
1064
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
1675
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
3589
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
7391
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7802
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...
0
7746
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5962
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
4941
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
3443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1869
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
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.