JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q55300F
  • License ISC

Package Exports

  • nest-object-deep-copy

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nest-object-deep-copy) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Deep Copy Nested Objects

Coverage badge gree

One confusion in javascript is hard copy / shallow copy objects. Usually developers are being told to use the spread operator, Object.assign or JSON.parse(JSON.stringify(object)) to get a real copy from the original object.

In most situations they will work as expected, but in certain circumstances they will not work as might you expect.

This javascript module aims to avoid problems that these methods have and always give you a real hard copy based on the original object.

Problem with spread operator and Object.assign()

If the object is a plain object and has only primitive values:

let user = {
  id: 1,
  gender: 'male'
};

Then the spread operator or Object.assign() will give you a hard copied object, let us continue with the example above:

let copiedUser = {
  ...user
};

// Change a property
copiedUser.id = 2;

console.log(user.id); // 1
console.log(copiedUser.id); // 2

// CopiedUser object property value change does not have an impact on the original user object

However, if the original object has a property that refers to a nested object, for example:

let user = {
  id: 101,
  gender: 'male',
  personalInfo: {
    name: 'Jack',
  }
};

Then the spread operator or Object.assign() WILL NOT give you a hard copied object, let us continue with another example:

let copiedUser = {
  ...user
};

// Change a nested object value
copiedUser.personalInfo.name = 'Tom';

// Change a property which holds a primitive value
copiedUser.id = 2;

// original user object mutation happens
console.log(user.personalInfo.name); // 'Tom'
console.log(copiedUser.personalInfo.name); // 'Tom'

// BUT mutation does not happen to property which holds a primitive value
console.log(user.id); // 1
console.log(copiedUser.id); // 2

Problem with JSON.parse(JSON.stringify(object))

JSON.parse(JSON.stringify(object)) WILL LOSE the property which equals to a function, for example:

let user = {
  id: 1,
  name: 'jack',
  speak: function() {
    console.log('I am speaking from original object.');
  }
};

var copiedUser = JSON.parse(JSON.stringify(user));

user.speak(); // `I am speaking from original object.`
copiedUser.speak(); //Uncaught TypeError: copiedUser.speak is not a function

Install

$ npm install nest-object-deep-copy

Hot to use it

const nestedHardCopy = require('nest-object-deep-copy');

// original object
let user = {
  id: 1,
  gender: 'male',
  personalInfo: {
    name: 'Jack',
  },
  speak: function() {
    console.log('I am speaking from original object.');
  }
};

//Get a hard copy
var copiedUser = nestedHardCopy(user);

//Change some values in the copied object
copiedUser.id = 2; //primitive property change
copiedUser.personalInfo.name = 'Daniel'; //nested object value change

copiedUser.speak = function() {
  console.log('I am speaking from copied object.')
}; //Assign a new function

console.log(user.id); // 1
console.log(user.personalInfo.name); // 'Jack'
user.speak(); // 'I am speaking from original object.'
copiedUser.speak(); //'I am speaking from copied object.'

License

This project is licensed under the ISC License