Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Little problem with Arrays

Status
Not open for further replies.

Frihi

Programmer
Jun 24, 2001
48
CH
Dings1=[];
Dings3=[1,2,3];
Dings4=[3,4,5];
Dings1=Dings3.concat(Dings4);
returns: 1,2,3,3,4,5.

But I want just 1,2,3,4,5. Of course at runtime I don't know if there are any double values but it might be so.

I suppose somebody can help me. Thanks.
 
Try this:

Code:
Dings1 = [];
Dings3 = [1, 2, 3];
lastDings3 = Dings3.length-1;
Dings4 = [3, 4, 5];
if ((Dings3[lastDings3]) == (Dings4[0])) {
	tmp = Dings3;
	tmp.pop();
	Dings1 = tmp.concat(Dings4);
} else {
	Dings1 = Dings3.concat(Dings4);
}[code]
 
I may not have explained it clearly. The elements of the arrays can change, so sometimes there are repeated values, sometimes not. Your code only takes the last elemant of that array to compare, but I need to compare all values. Any values.
Thanks anyway.
 
This is a duplicate checking function I made:

Code:
function checkDup (arr) {
	var chk = new Array();
	var dup = false;
	for (var i = 0; i<arr.length; i++) {
		for (var x = 0; x<chk.length; x++) {
			if (chk[x] == arr[i]) {
				dup = true;
			}
		}
		if (!dup) {
			chk[chk.length] = arr[i];
		}
		dup = false;
	}
	return chk;
}
var foo;
var foo1 = new Array(1, 2, 3);
var foo2 = new Array(3, 4, 5);
foo = foo1.concat(foo2);
foo = checkDup(foo);
foo.sort();
for (var i = 0; i<foo.length; i++) {
	trace (foo[i]);
}
Regards
David Byng
bd_logo.gif

davidbyng@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top