views:

91

answers:

4

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
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.
I'm confused how this would not work?
Byron Cobb
@Angelo R.: you're right: "there is no arr3."
BoltClock
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
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
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
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
@Byron - Why not use jsFiddle? - http://jsfiddle.net/WHdLp/
Peter Ajtai
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
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
+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
Quite elegant. I like this.
Peter Ajtai
don't care about the order. this worked fine. thx
FALCONSEYE
+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:

  1. array1.concat(array2) - concatenate the two arrays
  2. array1.concat(array2).unique() - return only the unique values. Here unique() is a method you added to the prototype for Array.

The whole thing would look like this:

jsFiddle example

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
+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
Careful with sort! `[500, 3, 9].sort()` will produce `[3, 500, 9]`! ... It's lexographic: http://jsfiddle.net/NLW4v/
Peter Ajtai
look for `sort` on w3schools, they have a tweak for numebrs.
Sirber
Either way, I'm not sure why the down votes. The answer provides a solution to the authors specific problem.
krio