473,385 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 18568


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 this textbox that has a bunch of data in it. At the...
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 array(5) = 6 array(6) = 6
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 easy way to do this? I have tried looping through...
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 returns all the unique values in a certain column...
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 it to work.) Public Function Unique(ByVal...
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 want to show all search strings that didn't...
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 removing duplicates. I have tried built in php...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.