I have a list of projects as followings:
projects = [{name: '1',
agencies: ['USA', 'China']},
{name: '2',
agencies: ['Japan', 'Russia']}]
I want to get the result ['USA', 'China', 'Japan', 'Russia'], currently I use the following code, but I would like to know if there is better solution (one line code) using map function:
const agencies = [];
projects.forEach((project) =>
project.agencies.forEach((agency) => {
agencies.push(agency);
})
);
const agencies = [].concat(...projects.map(x => x.agencies))for a one liner.var agencies = projects.reduce((arr, p) => [...arr, ...p.agencies], []):D