Say I have an array of [34,35,45,48,49] and another array of [48,55]. How can I get a resulting array of [34,35,45,48,49,55]?
+3
A:
function unique(arrayName)
{
var newArray=new Array();
label:for(var i=0; i<arrayName.length;i++ )
{
for(var j=0; j<newArray.length;j++ )
{
if(newArray[j]==arrayName[i])
continue label;
}
newArray[newArray.length] = arrayName[i];
}
return newArray;
}
var arr1 = new Array(0,2,4,4,4,4,4,5,5,6,6,6,7,7,8,9,5,1,2,3,0);
var arr2= new Array(3,5,8,1,2,32,1,2,1,2,4,7,8,9,1,2,1,2,3,4,5);
var union = unique(arr1.concat(arr2));
Byron Cobb
2010-09-02 18:01:56
Maybe I'm just confused, but how would this work? You're passing arr3, but there is no arr3, and even if there was it doesn't look like it's actually a union on arr1 | arr2 like he requested?
Angelo R.
2010-09-02 18:06:28
I'm confused how this would not work?
Byron Cobb
2010-09-02 18:07:26
@Angelo R.: you're right: "there is no arr3."
BoltClock
2010-09-02 18:10:12
var arr2 = []; if (currTaskIDs != '') { if( $.inArray(currTaskIDs, arr2) == -1) { arr2.push(currTaskIDs); } arr2 = unique(arr2.concat(arr)); } } now, currTaskIDs is = 34,35,45,48,49if i select say 50, it removes all of them and just displays 50
FALCONSEYE
2010-09-02 18:11:52
by the way, if i just use this: var currTaskIDs = $("#taskIDList").val(); // begin: create the task list: if (currTaskIDs != '') { if( $.inArray(currTaskIDs, arr) == -1) { arr.push(currTaskIDs); } arr = unique(arr); } it works perfectly first time. if i add another value to my currTaskIDs, then it becomes something like : 34,35,45,48,49,50,34,35,45,48,49,50,38
FALCONSEYE
2010-09-02 18:14:57
try this : if( $.inArray(currTaskIDs, arr) == -1) { arr.push(currTaskIDs); } var AnotherNewArr = unique(arr); Without seeing full code, i cant be sure what you're doing exactly, but it may be a clearing issue.
Byron Cobb
2010-09-02 18:19:47
I've tried this function at http://www.squarefree.com/jsenv/ and added print(union); and it works fine, even after adding more data to the array
Byron Cobb
2010-09-02 18:24:15
@Byron - Why not use jsFiddle? - http://jsfiddle.net/WHdLp/
Peter Ajtai
2010-09-02 18:30:49
this is driving me nuts but still no luck. this is my current code minus the unique function above: var tid = $(this).attr('name').split('-')[1]; var j = $(this).attr('name').split('-')[2]; var currTaskIDs = $("#taskIDList").val(); // begin: create the task list: var arr2 = []; if (currTaskIDs != '') { if( $.inArray(currTaskIDs, arr) == -1) { arr.push(currTaskIDs); } arr2 = unique(arr); alert(arr2); } if( $.inArray(tid, arr) == -1) { arr.push(tid); } the very first time i pick a new selection it works.
FALCONSEYE
2010-09-02 18:34:12
but if i go back and add another selection then it becomes 4,35,45,48,49,36,34,35,45,48,49,36,37,34,35,45,48,49,36,34,35,45,48,49,36,37,51
FALCONSEYE
2010-09-02 18:34:42
+3
A:
If you don't need to keep the order, and consider 45
and "45"
to be the same:
function union_arrays (x, y) {
var obj = {};
for (var i = x.length-1; i >= 0; -- i)
obj[x[i]] = x[i];
for (var i = y.length-1; i >= 0; -- i)
obj[y[i]] = y[i];
var res = []
for (var k in obj) {
if (obj.hasOwnProperty(k)) // <-- optional
res.push(obj[k]);
}
return res;
}
alert(union_arrays([34,35,45,48,49], [44,55]));
// shows [49, 48, 45, 35, 34, 55, 44]
KennyTM
2010-09-02 18:05:45
+1
A:
I would first concatenate the arrays, then I would return only the unique value.
You have to create your own function to return unique values. Since it is a useful function, you might as well add it in as a functionality of the Array
.
In your case with arrays array1
and array2
it would look like this:
array1.concat(array2)
- concatenate the two arraysarray1.concat(array2).unique()
- return only the unique values. Hereunique()
is a method you added to the prototype forArray
.
The whole thing would look like this:
Array.prototype.unique = function () {
var r = new Array();
o:for(var i = 0, n = this.length; i < n; i++)
{
for(var x = 0, y = r.length; x < y; x++)
{
if(r[x]==this[i])
{
continue o;
}
}
r[r.length] = this[i];
}
return r;
}
var array1 = [34,35,45,48,49];
var array2 = [34,35,45,48,49,55];
// concatenate the arrays then return only the unique values
alert(array1.concat(array2).unique());
Peter Ajtai
2010-09-02 18:06:07
+2
A:
First set your two arrays. Merge them into a new array. Make the new array unique and then sort it.
var first = [34,35,45,48,49];
var second = [48,55];
var third = $.merge( $.merge([],first), second);
var elem = $.unique(third).sort();
alert(elem);
krio
2010-09-02 18:08:02
Careful with sort! `[500, 3, 9].sort()` will produce `[3, 500, 9]`! ... It's lexographic: http://jsfiddle.net/NLW4v/
Peter Ajtai
2010-09-02 18:20:33
Either way, I'm not sure why the down votes. The answer provides a solution to the authors specific problem.
krio
2010-09-02 18:50:01