I have a TypeScript array of objects with the following structure:
const surveyResponses: any[] = [
{createdAt: new Date(), responses: [{questionId: 'input1', response: 'Bla'}]},
{createdAt: new Date(), responses: [{questionId: 'input1', response: 'Blo'}]}
];
I would like to extract the response
property inside of the responses
array and have them as a single array of strings.
Expected result: ['Bla', 'Blo']
I have tried to follow these advices:
- From an array of objects, extract value of a property as array
- Returning only certain properties from an array of objects in Javascript
I have tried to use reduce
without luck:
responses.reduce((a, o) => (o.responses.map(r => r.response) && a.push(o.responses.map(r => r.response)), a), []);
I have tried to use map
without luck:
responses.map(r => r.responses).map(r => r.map(r => r.response));
Result: [["Bla"], ["Blo"]]
Any help would be appreciated.