Merge an array of objects and concatenate values based on a function