Mistre83 Mistre83 - 5 months ago 137
Javascript Question

Lodash - Remove nested property from object

I have an array of objects like this:

[{
createdAt: "a date",
id: "my id",
registered: {
__type: 'Date',
iso: 'the date'
}
}]


Because the field registered is an object and what i really just need is the
iso
field, there is a fast way in Lodash (or.... in javascript) to transform this array of object in another like this ?

[{
createdAt: "a date",
id: "my id",
registered: 'the date'
}]


I did tried something like this... but do not works...

const newObj = _.values(measurements).map(element => {
_.omit(element.registered['__type'])
});

Answer

If you don't mind mutating the existing object, .forEach is all you need. Simply override that property, there is no need to delete anything:

arr.forEach(function(obj) {
  obj.registered = obj.registered.iso;
});