2

I need to create a new object from another. For example. This is an old object:

const objectOld = { 
  level1: { 
    level2: { 
      level3: { 
        code: 123, 
        error: 'Message' 
      }  
    } 
  } 
}

and this my new object which created based on the old object.

const objectNew = { 
  level1: { 
    level2: { 
      level3: { 
        error: {
           code: 123, 
           key: 'Message' 
        }
      }  
    } 
  } 
}

The depth may be different, of course.

Can someone help me?

Amel
  • 708
  • 6
  • 17
Anna
  • 23
  • 4
  • Does this answer your question? [What is the most efficient way to deep clone an object in JavaScript?](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript) – Amel Nov 14 '19 at 14:53

4 Answers4

0

Do you just want to copy that object? For deep copy you can use

const newObj = JSON.parse(JSON.stringify(oldObj))
maciejze
  • 177
  • 7
  • No, it's not only copy. I know how to copy. It's how to create a new object partially based on old. Just look into old and new object. – Anna Nov 14 '19 at 19:41
0

Use lodash javascript library for deep copy.


const objectOld = { 
  level1: { 
    level2: { 
      level3: { 
        code: 123, 
        error: 'Message' 
      }  
    } 
  } 
}

var objectNew = _.cloneDeep(objectOld);

console.log(objectNew === objectOld)

Roy
  • 1,189
  • 7
  • 13
  • No, it's not only copy. I know how to copy. It's how to create a new object partially based on old. Just look into old and new object. – Anna Nov 14 '19 at 19:41
0

You can accomplish this with recusion, Array#reduce, Object#entries, some destruction.

This will deep copy your object and will create the { error: { code: string, key: string } } if it detects an object with code and message properties.

If the object does not have those properties, it'll self call itself and continue.

If the value is not an object, keep the value as is.

const objectOld = { 
  level1: { 
    level2: { 
      level3: { 
        code: 123, 
        error: 'Message' 
      }  
    } 
  } 
};


function transform(obj){
  return Object
  .entries(obj)
  .reduce((acc, [key, value])=>{
    if(typeof value === 'object'){
      if("code" in value && "error" in value){
        acc[key] = {
          error: {
            key: value.error,
            code: value.code
          }
        }
      } else {
        acc[key] = transform(value)
      }
    } else {
      acc[key] = value;
    }
    return acc;
  }, {});
}

const objectNew = transform(objectOld);

console.log(
  objectNew
);
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
0

You can use a generic utility that calls a transformation function for each key-value pair in the object and apply it recursively:

function transform(obj, fn) {
    function transformer(obj) {
        if (!obj || typeof obj !== 'object')
            return obj;
        if (Array.isArray(obj))
            return obj.map((v, k) => fn(v, k, transformer));
        return Object.fromEntries(
            Object.entries(obj).map(([k, v]) => [k, fn(v, k, transformer)]));
    }
    return transformer(obj)
}

//

const objectOld = {
    level1: {
        level2: {
            level3: {
                code: 123,
                error: 'Message'
            }
        }
    },
    otherKey: { anotherOne: { code: 25, error: 'Message2'}}
}


let newObj = transform(objectOld, (val, key, trans) =>
    ('error' in val) ? {error: val} : trans(val)
)

console.log(newObj)
georg
  • 211,518
  • 52
  • 313
  • 390
  • Good solution, thanks! Unfortunately I can't set your answer as a solution too. Only one answer :/ – Anna Nov 14 '19 at 19:53