Add multiple attributes to an existing js object

前端 未结 6 1033
不知归路
不知归路 2020-12-31 01:50

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         


        
6条回答
  •  不知归路
    2020-12-31 02:10

    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);

提交回复
热议问题