Javascript update and ignoring undefined fields

I want to substantially update a large javascript object. However, various tools at the front add unnecessary margins that should be ignored when updating the object. I'm working with angular, but open up to use lodash / underscore if needed. I just want to be able to update an existing object without adding additional fields that are entered when they are placed in the UI.

var ob1 = {
    attr1: 'stuff',
    attr2: 'stuff'
};

var ob2 = {
    attr1: 'changedstuff',
    attr2: 'stuff',
    uiCrap: 'junk'
};

update(ob1, ob2);
// should result in 
// ob1 = { attr1: 'changedstuff', attr2: 'stuff };

      

Does anyone know if it has a built in angular / javascript function?

+3


source to share


1 answer


Use the extend () function :



var ob = _.extend(ob1, _.pick(ob2, _.keys(ob1)));

      

+3


source







All Articles