...

# Combine two lists

ghosty
12-20-2006, 12:21 AM
In JavaScript, how could I take two lists such as:

var listA = ['Jim','Steve','Jill','Tom','Bob'];
var listB = ['Steve','Bob','Bill','Dave'];

and unify them, putting both the unique and shared names of each list into a new list, like:

Jim, Steve, Jill, Tom, Bob, Bill, Dave

Any ideas would be appreciated, thanks

david_kw
12-20-2006, 12:57 AM
How long will these lists typically be and how often will it be called? If they are short 2 nested loops would work fine. If they are long and called often it should probably be optimized.

function makeOneList(list1, list2) {
var ret = [].concat(list1);

for (var i = 0; i < list1.length; i++) {
var j;
for (j = 0; j < list2.length; j++) {
if (ret[i] == list2[j]) {
break;
}
}
if (j >= list2.length) {
ret.concat(list2[j]);
}
}

return(ret);
}

Of course, I don't have time to test it but the basic idea should be correct.

david_kw

ghosty
12-20-2006, 01:32 AM
I tried that and it only returned list1
What I want to happen is for it to return all the entries of list1 and list2, with no repetitions. That is, compare the two in order to make them one single list.

dumpfi
12-20-2006, 06:02 PM
Object.prototype.keys = function()
{
var
x,
keys = [];

for(x in this)
{
if(!(x in this.__proto__))
{
keys.push(x);
}
}
return keys;
}
Array.prototype.unique = function()
{
var
map = {},
i = this.length - 1;

for(; i >= 0; --i)
{
map[this[i]] = 1;
}
return map.keys();
}

var
listA = ['Jim','Steve','Jill','Tom','Bob'],
listB = ['Steve','Bob','Bill','Dave'],
listAll, listUnique;

// make one list with all names
listAll = listA.concat(listB);
// remove duplicates
listUnique = listAll.unique();

david_kw
12-20-2006, 06:42 PM
Whoops, I had mine all backwards. Here is the fixed version.

function makeOneList(list1, list2) {
var ret = [].concat(list1);

for (var i = 0; i < list2.length; i++) {
var j;
for (j = 0; j < ret.length; j++) {
if (ret[j] == list2[i]) {
break;
}
}
if (j >= ret.length) {
ret.push(list2[i]);
}
}

dumpfi's might be better. I didn't try it out. I wanted to fix mine in any case.

david_kw

david_kw
12-20-2006, 06:59 PM
Ok I tried out dumpfi's. Pretty tricky stuff. It works fine in FF but in IE I get an error that

this.__proto__

is not an object. Is there an IE equivilent? Or did I mess something up?

To get it to work in IE I had to change

if(!(x in this.__proto__))

to

if (x != "keys")

That made it work in both browsers for this test. I suspect if you added another prototype to the object you'd have to object make sure that wasn't included as well.

So is there a way to see if a key is a prototype in IE?

david_kw

liorean
12-20-2006, 07:31 PM
Only SpiderMonkey exposes the internal [[prototype]] property as __proto__. The other browser scripting engines don't allow this kind of access. Use Object.prototype.hasOwnProperty instead.

david_kw
12-20-2006, 07:50 PM
Thanks, just what I was curious about. The if statement can be

if (this.hasOwnProperty(x))

now and works in both IE and FF.

david_kw