Guest User

Untitled

a guest
Jan 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. const deepEqual = (firstObject, secondObject) => {
  2. if(firstObject == secondObject){return true}
  3. if(typeof firstObject != typeof secondObject){return false}
  4. if(firstObject instanceof Array && secondObject instanceof Array){return compareArrays(firstObject, secondObject)}
  5. if(firstObject instanceof Object && secondObject instanceof Object){return compareObjects(firstObject, secondObject)}
  6. return false;
  7. }
  8.  
  9. const compareArrays = (firstArray,secondArray) => {
  10. if(firstArray.length != secondArray.length){
  11. return false
  12. }
  13. var judge = true
  14. firstArray.forEach(function(value, index){
  15. if (judge == false){
  16. return judge
  17. }
  18. if(value == secondArray[index]){
  19. judge = true
  20. }
  21. else {
  22. judge = false;
  23. }
  24. })
  25. return judge;
  26. }
  27.  
  28. const compareObjects = (firstObject, secondObject) => {
  29. var valueFirst = Object.values(firstObject)
  30. var valueSecond = Object.values(secondObject)
  31. var propFirst = Object.keys(firstObject)
  32. var propSecond = Object.keys(secondObject)
  33. if (compareArrays(valueFirst, valueSecond)){
  34. if (compareArrays(propFirst, propSecond)){
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40.  
  41. console.log(deepEqual(obj, obj1))
  42. //obj1 = {lucas: 'sales', machado: 'lucas'}
  43. //obj= {lucas: 'sales', machado: 'lucas'}
  44. //true
  45.  
  46. //obj1 = {lucas: 'sales', machado: 'luas'}
  47. //obj= {lucas: 'sales', machado: 'lucas'}
  48. //false
  49.  
  50. //obj1 = 5
  51. //obj= 8
  52. //false
  53.  
  54. //obj1 = [1,2,3,4,5]
  55. //obj= [1,2,3,4,5]
  56. //true
  57.  
  58. //obj1 = [1,2,3,4,5]
  59. //obj= [1,2,3,4,5,6]
  60. //false
Add Comment
Please, Sign In to add comment