Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. const { assign, cloneDeep } = require('lodash');
  2.  
  3. let obj = {
  4. type: {
  5. values: {
  6. results: [1, 2]
  7. }
  8. }
  9. };
  10. let copy;
  11.  
  12. // simple assignment => doesn't work
  13. copy = obj;
  14. copy
  15. copy.type.values.results.push(3);
  16. console.log(JSON.stringify(copy))
  17. console.log(JSON.stringify(obj))
  18.  
  19. // spread => mutation => doesn't work
  20. obj = {
  21. type: {
  22. values: {
  23. results: [1, 2]
  24. }
  25. }
  26. };
  27. copy = {
  28. ...obj
  29. }
  30. copy
  31. copy.type.values.results.push(3);
  32. console.log(JSON.stringify(copy))
  33. console.log(JSON.stringify(obj))
  34.  
  35. // lodash assing => mutation => doesn't work
  36. obj = {
  37. type: {
  38. values: {
  39. results: [1, 2]
  40. }
  41. }
  42. };
  43. copy = assign(obj, {});
  44. copy
  45. copy.type.values.results.push(3);
  46. console.log(JSON.stringify(copy))
  47. console.log(JSON.stringify(obj))
  48.  
  49. // cloneDeep => works
  50. obj = {
  51. type: {
  52. values: {
  53. results: [1, 2]
  54. }
  55. }
  56. };
  57. copy = cloneDeep(obj);
  58. copy
  59. copy.type.values.results.push(3);
  60. console.log(JSON.stringify(copy))
  61. console.log(JSON.stringify(obj))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement