0

I'm trying to merge 2 objects into 1 object without changing the order.

For example:

obj1 = {"-":"-","<1":"test"};    
obj2 = {"1":"1","2":"2"};

The output that i wanna generate:

obj3 = {"-":"-","<1":"test","1":"1","2":"2"};    

I've tried using:

  • add & concat => fail, coz it's generates an array of objects
  • extend => almost, somehow obj1 is put after obj2 so it become like this:

    obj3 = {"1":"1","2":"2","-":"-","<1":"test"};

  • merge without changing obj1 and obj2 into array => fail, it only returns me obj1

    result=$.merge( obj1, obj2 );

  • merge with first changing obj1 and obj2 into array => fail, it became array of objects

A lil enlightenment here? Thanks in advance.

UPDATE:

this is my code:

obj2 = {};
obj1 = {"-":"-","<1":"test"};

for(x = 1;x<=2;x++){
   obj2[x] = x;
}

obj3 = $.extend(obj1, obj2);
return obj3;

Apparently, when we print the obj3 in console it prints as:

obj3 = {"1":"1","2":"2","-":"-","<1":"test"};

but actually, when u process the obj3 return in another function, the order remains as in:

{"-":"-","<1":"test","1":"1","2":"2"};

Interesting.... Well problem solved. Thank you! Everyone gets a +1

  • 1
    I dont know if this will help, but the `merge()` documentation shows the merging of two objects in this fashion `$.merge( $.merge([],first), second);` (http://api.jquery.com/jQuery.merge/) – Dutchie432 Oct 19 '11 at 10:15
  • 3
    Objects shouldn't have a defined order. That is what arrays are for. – alex Oct 19 '11 at 10:17
  • 1
    how is index important for objects. you have an object, not an array. and "-", "<1" etc are properties of that object – naveen Oct 19 '11 at 10:19
  • good reference: http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically – naveen Oct 19 '11 at 10:24
  • @ Duchie: it appears to me merge only works for arrays not objects; @ Alex : perhaps you're right, but i need the an assoc array; @ Haveen: yea, i've been to that thread. and to treat the "-" and "<1" as properties might be a good idea; Thanks to all of you! – NineAllexis Oct 19 '11 at 10:35

1 Answers1

1

This is not possible as objects are not ordered. If you really, really, really need ordering, you can use an array with key/value pairs as separate elements.

For example:

var x = ['key', 'value', 'otherkey', 'othervalue'];
var y = ['ykey', 'yvalue'];

var z = x.concat(y);

Of course, with this, you'd want to have some utility functions:

Array.prototype.to_object = function() {
  if(this.length % 2 != 0) {
    //uneven, abort.
    return null;
  }

  var obj = {};
  for(var i = 0, j = this.length; i < j; i += 2) {
     obj[this[i]] = this[i+1];
  }
  return obj;
};

Object.prototype.to_keyed_array = function() {
  if(this instanceof Array) return null;

  var array = [];
  var i = 0;
  for(var k in this) {
     if(this.hasOwnProperty(k)) {
        array[i] = k;
        array[i+1] = this[k];
        i += 2;
     }
  }
  return array;
};

NOTE: These functions, again, do not guarantee ordering when converting from an object to an Array and vice versa. Their sole purpose is to give you a bridge between the two paradigms if you will.

Example:

['k1', 'v1', 'k2', 'v2'].to_object(); //-> {'k1': 'v1', 'k2':'v2'}
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320