I have this sample data
let data = [{
id: 1,
info: [
'Optical',
'Drive'
]
},
{
id: 2,
info: [
'Paper',
'Binder'
]
}
];
I want to convert the array in data.info to object and assign 2 key/value pairs, first key is name with value from data.info and 2nd key is the length of data.info string. The result would be something like this:
let data = [{
id: 1,
info: [{
name: 'Optical',
length: 7
},
{
name: 'Drive',
length: 5
},
]
},
{
id: 2,
info: [{
name: 'Paper',
length: 5
},
{
name: 'Binder',
length: 6
}
]
}
];
Here's what I've tried so far
const getLength = (str) => (str.length)
const addLength = (str) => {
try {
let newObj = {};
newObj.name = str;
newObj.length = getLength(str);
return newObj;
} catch (error) {
console.log(error)
}
}
const mapedData = data.map((object) => {
object.info.map((str) => (addLength(str)))
})
However the result is
[ undefined, undefined ]
Any idea how to fix it?
returnmust be explicit.