34

I want to add multiple attributes to an existing object with existing attributes. Is there a more concise way than one line per new attribute?

myObject.name = 'don';
myObject.gender = 'male';

Everything on MDN shows how to do new objects with bracket notation, but not existing objects: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects

Don P
  • 60,113
  • 114
  • 300
  • 432
  • 2
    You could make a function that receives your object and an object with the new props/values, and enumerates the new object, updating the old one. That'll be a little more concise. `update(obj, {name:"don", gender:"male"})` –  May 07 '13 at 19:49

6 Answers6

51

In ES6/ ES2015 you can use the Object.assign method

let obj = {key1: true};
console.log('old obj: ', obj);
let newObj = {key2: false, key3: false};

Object.assign(obj, newObj);
console.log('modified obj: ', obj);
Ado
  • 637
  • 1
  • 6
  • 17
Ed Barahona
  • 721
  • 5
  • 7
20

I'm not sure if that's helpful, but there is a trick using JS ES6 spread syntax:

let obj = {a: 1, b: 2};
obj = {...obj, b: 3, c: 4};
console.log(obj); // {a: 1, b: 3, c: 4}

You can try to use this somehow... In general, that's a single liner to add or override object properties using js destructuring method.

Edit: Notice that the placement of ...obj in the new object is cruicial, as placing it first would allow overriding it with the following params (b: 3, c: 4 in my example), and placing it last will give priority to the members that are already in the object.

Shay Yzhakov
  • 975
  • 2
  • 13
  • 31
8

From How can I merge properties of two JavaScript objects dynamically?

var obj2 = {name: 'don', gender: 'male'};
for (var attrname in myobject) { myobject[attrname] = obj2[attrname]; }

EDIT

To be a bit clearer about how you could extend Object to use this function:

//Extend the protype so you can make calls on the instance
Object.prototype.merge = function(obj2) {
    for (var attrname in obj2) {
        this[attrname] = obj2[attrname];
    }
    //Returning this is optional and certainly up to your implementation.  
    //It allows for nice method chaining.
    return this;
};
//Append to the object constructor function so you can only make static calls
Object.merge2 = function(obj1, obj2) {
    for (var attrname in obj2) {
        obj1[attrname] = obj2[attrname];
    }
    //Returning obj1 is optional and certainly up to your implementation
    return obj1;
};

Usage:

var myObject1 = { One: "One" };
myObject1.merge({ Two: "Two" }).merge({ Three: "Three" });
//myObject1 is { One: "One", Two: "Two", Three: "Three", merge: function }


var myObject2 = Object.merge2({ One: "One" }, { Two: "Two" });
Object.merge2(myObject2, { Three: "Three" });
//myObject2 is { One: "One", Two: "Two", Three: "Three" }

Note: You certainly could implement a flexible merge conflict strategy depending on your needs.

Community
  • 1
  • 1
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80
7

Use jQuery library

jQuery.extend(myObject, { name : 'don', gender : 'male' });
zavg
  • 10,351
  • 4
  • 44
  • 67
6

Sometimes I do it like this:

var props = {
   name : 'don',
   gender : 'male',
   age : 12,
   other : false
};
for(var p in props) myObject[p] = props[p];
letiagoalves
  • 11,224
  • 4
  • 40
  • 66
3

In ECMAscript 5 you can make us of defineProperties:

Object.defineProperties(myobject, {  
  name: {  
    value: 'don' 
  },  
  gender: {  
    value: 'male' 
  }  
});
JimmyRare
  • 3,958
  • 2
  • 20
  • 23
  • Unfortunately that syntax is pretty verbose, making it even longer than the original that OP is hoping to streamline. –  May 07 '13 at 19:55
  • 1
    Yeah, I guess you're right, this has more use when you want to protect against mutation of your objects. Hehe,I always do that... =P – JimmyRare May 07 '13 at 19:57
  • 1
    I agree. It's a nice feature, just clunky. –  May 07 '13 at 19:59
  • Also worth checking the ECMAScript v5 browser compatibility depending on your target audience - http://kangax.github.io/es5-compat-table/ - in this case nothing below IE9. – William Turrell Feb 21 '14 at 21:20