Javascript - deepEqual Comparison

后端 未结 8 1283
囚心锁ツ
囚心锁ツ 2020-12-05 05:21

Question (From Eloquent Javascript 2nd Edition, Chapter 4, Exercise 4):

Write a function, deepEqual, that takes two values and returns true only if th

8条回答
  •  抹茶落季
    2020-12-05 05:46

    I just went through this chapter and wanted to show my work, too.

    The flaw in mine (let me know if there are more) is that the object properties have to be in exact order as well. I much prefer @paul and @danni's solution.

    // Deep equal 
    const deepEqual = (x, y) => {
      const xType = typeof x;
      const yType = typeof y; 
      
      if ( xType === 'object' && yType === 'object' && ( x !== null && y !== null ) ) {
        const xKeys = Object.keys(x);
        const yKeys = Object.keys(y);
        const xValues = Object.values(x);
        const yValues = Object.values(y);  
        
        // check length of both arrays
        if ( xKeys.length !== yKeys.length ) return false;
        
        // compare keys
        for ( i = 0; i < xKeys.length; i++ )
          if (xKeys[i] !== yKeys[i]) return false;
          
        // compare values
        for ( i = 0; i < xValues.length; i++ )
          if (!deepEqual(xValues[i], yValues[i])) return false;
          
      } else {
        if ( x !== y ) return false;
      }
      return true;
    };
    
    // Objects
    let obj1 = {
      value: false,
      pets: null
    };
    
    let obj2 = {
      value: false,
      pets: null
    };
    
    
    let obj3 = {
      value: false,
      pets: {
        cat: false,
        dog: {
          better: 'yes'
        }
      }
    };
    
    let obj4 = {
      value: false,
      pets: { 
        cat: false,
        dog: {
          better: 'yes'
        }
      }
    };
    
    
    let obj5 = {
      value: false,
      dog: true
    };
    
    let obj6 = {
      value: false,
      cat: true
    };
    
    
    let obj7 = {
      value: true,
      dog: {
        cat: {
          wow: true
        }
      }
    };
    
    let obj8 = {
      value: true,
      dog: {
        cat: {
          wow: false
        }
      }
    };
    
    
    let obj9 = {
      value: true,
      dog: {
        cat: {
          wow: true
        }
      }
    };
    
    let obj10 = {
      dog: {
        cat: {
          wow: true
        }
      },
      value: true
    };
    
    // Just for building a pretty result, ignore if you'd like
    const result = (x, y) => {
      return `For: 
    ${JSON.stringify(x)}
    and
    ${JSON.stringify(y)}
    >> ${deepEqual(x, y)}`; }; // To print results in const resultDivs = document.querySelectorAll('.result'); resultDivs[0].innerHTML = result(obj1, obj2); resultDivs[1].innerHTML = result(obj3, obj4); resultDivs[2].innerHTML = result(obj5, obj6); resultDivs[3].innerHTML = result(obj7, obj8); resultDivs[4].innerHTML = result(obj9, obj10);
    body {
      font-family: monospace;
    }
    
    span {
      color: #a0a0a0;
    }
    
    .result {
      margin-bottom: 1em;
    }

提交回复
热议问题