Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Combine two lists

    In JavaScript, how could I take two lists such as:

    Code:
    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

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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

  • #3
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply
    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.
    Last edited by ghosty; 12-20-2006 at 12:36 AM.

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    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();
    
    alert(listUnique);
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #5
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whoops, I had mine all backwards. Here is the fixed version.

    Code:
      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

  • #6
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeċ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •