[Vuejs]-How to effeciently return a subarray by key

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)

Leave a comment