I am trying to use a lodash's map function to extract a value from a nested object in an array of objects and store it in a new key. But always got stuck with lodash and am getting an error. Any help would be much appreciated. The key nested will always be an array of 1 object.
const _ = require('lodash');
var all_data = [{id: 'A', nested: [{id: '123'}]}, {id: 'B', nested: [{id: '456'}]}];
_.map(all_data, (data) => ({
data.nested_id = data.nested[0].id;
}));
console.log(all_data)
Desired output: [{id: 'A', nested_id: '123', nested: [{id: '123'}]}, {id: 'B', nested_id: '456', nested: [{id: '456'}]}]
datafrom the callback of_.mapbut this can be done in pure JS and without mutation:all_data.map((obj) => ({ ...obj, nested_id: obj.nested[0].id }))