Quiz 100% Result

  • is there any way to reverse the object in js?

    const initObject = { 
      value: 5,
      next: {
       value: 10,
       next: {
         value: 15
         next: null
       }
      },
    }
    
    //expected result
    
    const newObject = {
      value: 15,
      next: {
        value: 10,
        next: {
          value: 5,
          next: null
        }
      }
    }
    
    

    need to a function, but struggling hard.
    I tried to find the object depth-first and then make a founded amount of iterations for … in … inside the object but don’t know how to rewrite the new one

  • Cyberian's Gold

    @maria-irshad said in What is Reverse object hierarchy:

    is there any way to reverse the object in js?

    const initObject = { 
      value: 5,
      next: {
       value: 10,
       next: {
         value: 15
         next: null
       }
      },
    }
    
    //expected result
    
    const newObject = {
      value: 15,
      next: {
        value: 10,
        next: {
          value: 5,
          next: null
        }
      }
    }
    
    

    need to a function, but struggling hard.
    I tried to find the object depth-first and then make a founded amount of iterations for … in … inside the object but don’t know how to rewrite the new one

    Try this

    const initObject = { 
      value: 5,
      next: {
       value: 10,
       next: {
         value: 15,
         next: null
       }
      }
    }
    
    const getValues = ({ value, next }) =>
      next 
        ? [value, ...getValues(next)] 
        : [value]
    
    const createObject = values => 
      values.reduce((next, value) => ({ value, next }), null)
    
    const output = createObject(getValues(initObject))
    
    console.log(output)
    
  • Cyberian's Gold

  • Cyberian's Gold

    @maria-irshad said in What is Reverse object hierarchy:

    is there any way to reverse the object in js?

    const initObject = { 
      value: 5,
      next: {
       value: 10,
       next: {
         value: 15
         next: null
       }
      },
    }
    
    //expected result
    
    const newObject = {
      value: 15,
      next: {
        value: 10,
        next: {
          value: 5,
          next: null
        }
      }
    }
    
    

    need to a function, but struggling hard.
    I tried to find the object depth-first and then make a founded amount of iterations for … in … inside the object but don’t know how to rewrite the new one

    Try this

    const initObject = { 
      value: 5,
      next: {
       value: 10,
       next: {
         value: 15,
         next: null
       }
      }
    }
    
    const getValues = ({ value, next }) =>
      next 
        ? [value, ...getValues(next)] 
        : [value]
    
    const createObject = values => 
      values.reduce((next, value) => ({ value, next }), null)
    
    const output = createObject(getValues(initObject))
    
    console.log(output)
    

50% Off on Your FEE Join US!

Quiz 100% Result If you want to know how you can join us and get 50% Discout on your FEE ask Cyberian in Chat Room! Quiz 100% Result Quiz 100% Result
| |