7

I want to rename key in obj, from this:

objs = {
  one: { description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} },
  two: { description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} }
}

into this:

objs = {
  one: { original_description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} },
  two: { original_description: "value", amount: 5, value: { description: "value desc", identifier: "some text"} }
}

1 Answers1

13

You don't really need lodash for this. What you need to do is create a new key on your objects with the old value, and then delete the old key. For example:

Object.keys(objs).forEach(function (key) {
    objs[key].original_description = objs[key].description;
    delete objs[key].description;
});

If you're not familiar with the delete operator, see the MDN documentation for delete

Andrew Burgess
  • 1,765
  • 17
  • 19
  • 9
    This method is mutating the original object, find here an immutable version using Lodash https://gist.github.com/LeoAref/9e34436c8b140d690733631befc248ba – Muhammad Hamada May 19 '17 at 05:49