Created
          December 13, 2018 13:39 
        
      - 
      
 - 
        
Save dheerajsuthar/76cfb02b2721f666627c4b89a8967e8c to your computer and use it in GitHub Desktop.  
    Immutable version
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | let users = [ | |
| { | |
| name: { | |
| first: 'John', | |
| last: 'Galt' | |
| }, | |
| email: '[email protected]', | |
| age: 30 | |
| }, | |
| { | |
| name: { | |
| first: 'Mickey', | |
| last: 'Mouse' | |
| }, | |
| email: '[email protected]', | |
| age: 90 | |
| }, | |
| { | |
| name: { | |
| first: 'Charlie', | |
| last: 'Chaplin' | |
| }, | |
| email: '[email protected]', | |
| age: 129 | |
| }, | |
| ]; | |
| const ChangeUserFirstName = 'John', | |
| ChangeUserLastName = 'Galt', | |
| ChangeAge = 38; | |
| let CurrentDayOfBirth = null; | |
| // Following change spoils the existing object for everyone. Other users won't | |
| // have any way of knowing the original state. | |
| const freshUsers = users.map(user => { | |
| if (user.name.first === ChangeUserFirstName && | |
| user.name.last === ChangeUserLastName) { | |
| // Create a fresh user record. All the key from both the existing object | |
| // are copied in the empty object follwed by overwriting from incoming changes. | |
| return Object.assign({}, user, { age: ChangeAge }); | |
| } | |
| // Unmodified records. | |
| return user; | |
| }); | |
| console.log('users', users); | |
| console.log('freshUsers', freshUsers); | |
| // Find the changed record. | |
| const changedRecord = freshUsers.filter(user => !users.includes(user)); | |
| const originalRecord = users.filter(user => user.name.first === ChangeUserFirstName && | |
| user.name.last === ChangeUserLastName); | |
| console.log('changedRecord', changedRecord); | |
| console.log('originalRecord', originalRecord); | |
| // ============Output============ | |
| //   npx babel-node --presets @babel/preset-env immutability.js | |
| // users [ { name: { first: 'John', last: 'Galt' }, | |
| // email: '[email protected]', | |
| // age: 30 }, | |
| // { name: { first: 'Mickey', last: 'Mouse' }, | |
| // email: '[email protected]', | |
| // age: 90 }, | |
| // { name: { first: 'Charlie', last: 'Chaplin' }, | |
| // email: '[email protected]', | |
| // age: 129 } ] | |
| // freshUsers [ { name: { first: 'John', last: 'Galt' }, | |
| // email: '[email protected]', | |
| // age: 38 }, | |
| // { name: { first: 'Mickey', last: 'Mouse' }, | |
| // email: '[email protected]', | |
| // age: 90 }, | |
| // { name: { first: 'Charlie', last: 'Chaplin' }, | |
| // email: '[email protected]', | |
| // age: 129 } ] | |
| // changedRecord [ { name: { first: 'John', last: 'Galt' }, | |
| // email: '[email protected]', | |
| // age: 38 } ] | |
| // originalRecord [ { name: { first: 'John', last: 'Galt' }, | |
| // email: '[email protected]', | |
| // age: 30 } ] | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment