1๐
โ
A slick way to accomplish this would just be to reduce the initial array:
let result = myArr.reduce((a,b) => a.concat(b.myArr || []), []);
That starts with an empty array and just concats the selected property into it, resulting in a single array of objects. If the subarray is undefined
or otherwise falsy, we can concat
an empty array (which will not actually add anything to the original array)
Source:stackexchange.com