I was wondering if it is possible to destructure a nested object without knowing the key of the parent object?
The reason to do this is that mongoose validation errors returns an object in the errors
key and each entry of this object is another object where the key is the name of the field that has an error.
const data = {
name: {
path: 'name',
name: 'The error message'
},
email: {
path: 'email',
name: 'Another error message'
}
}
So the result can be something like
{
name: 'The error message',
email: 'Another error message'
}
Is it possible that the unique way to do this is using for in
loop and perform the destructuring assignment inside of it?
let message = {}
for (var k in data) {
var { path, name } = data[k]
message[path] = name
}