Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const deepEqual = (firstObject, secondObject) => {
- if(firstObject == secondObject){return true}
- if(typeof firstObject != typeof secondObject){return false}
- if(firstObject instanceof Array && secondObject instanceof Array){return compareArrays(firstObject, secondObject)}
- if(firstObject instanceof Object && secondObject instanceof Object){return compareObjects(firstObject, secondObject)}
- return false;
- }
- const compareArrays = (firstArray,secondArray) => {
- if(firstArray.length != secondArray.length){
- return false
- }
- var judge = true
- firstArray.forEach(function(value, index){
- if (judge == false){
- return judge
- }
- if(value == secondArray[index]){
- judge = true
- }
- else {
- judge = false;
- }
- })
- return judge;
- }
- const compareObjects = (firstObject, secondObject) => {
- var valueFirst = Object.values(firstObject)
- var valueSecond = Object.values(secondObject)
- var propFirst = Object.keys(firstObject)
- var propSecond = Object.keys(secondObject)
- if (compareArrays(valueFirst, valueSecond)){
- if (compareArrays(propFirst, propSecond)){
- return true;
- }
- }
- return false;
- }
- console.log(deepEqual(obj, obj1))
- //obj1 = {lucas: 'sales', machado: 'lucas'}
- //obj= {lucas: 'sales', machado: 'lucas'}
- //true
- //obj1 = {lucas: 'sales', machado: 'luas'}
- //obj= {lucas: 'sales', machado: 'lucas'}
- //false
- //obj1 = 5
- //obj= 8
- //false
- //obj1 = [1,2,3,4,5]
- //obj= [1,2,3,4,5]
- //true
- //obj1 = [1,2,3,4,5]
- //obj= [1,2,3,4,5,6]
- //false
Add Comment
Please, Sign In to add comment