Wednesday, April 11, 2018

Check to see if two objects have the same reference in JavaScript!

Do a strictly equals comparison with the === operator between the two objects. It won't matter is some of the properties have been changed on one of two matching actors because really if they have the same reference they both have the change as they are both the same thing with the same reference/pointer. Get it? Sometimes this trick makes more sense than explicitly putting id properties at objects to tell apart the contents of a collection. In the NgRx paradigm in an Angular 4 app wherein you do not wish to mutate state, EVER, how will you update just one piece of an array with a .map operation?

updateAttitudeAction = <UpdateAttitudeAction>action;
return {
   ...state,
   attitudeAndAdvocacy: {
      ...state.attitudeAndAdvocacy,
      attitudes: state.attitudeAndAdvocacy.attitudes.map(a =>
         a === updateAttitudeAction.attitudeReference
            ? { message: updateAttitudeAction.updatedAttitude }
            : a
      ),
      isDirty: true
   }

No comments:

Post a Comment