Combine data from array of objects I got this data structure and I am checking to see which properties are true, then I am adding the key for all the properties that are true to that object on a new property called combined


const data = [
  {
    keyword: 'banana',
    yellow: true,
    sweet: true
  },
  {
    keyword: 'pineapple',
    yellow: true,
    sweet: false
  },
  {
    keyword: 'apple',
    yellow: false,
    sweet: false
  },
]

const combined = [...data].reduce((acc, { keyword, ...rest}) =&gt; {
  const res = Object.entries(rest).reduce((total, [key, value]) =&gt; {
    if (value === true) total.push(key)
    return total
  }, [])
  acc.push(res)
  return acc
}, []).map(entry =&gt; ({ combined: entry.join(' ') }))

const output = data.map((entry, index) =&gt; ({
  ...entry,
  ...combined[index]
}))

console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }



It seems pretty straight forward but somehow this feels a bit convoluted
From a short review;

Your own answer does read better
map is the right approach
I would go for filter instead of reduce (you only want 'true' values that are not keyword)
I probably would have named total -&gt; keys
I prefer &lt;verb&gt;&lt;object&gt; so combined -&gt; addGroups
If you dont use a falsy comparison, then you dont need to check for 'keyword'



const data = [
    {keyword: 'banana', yellow: true, sweet: true},
    {keyword: 'pineapple', yellow: true, sweet: false},
    {keyword: 'apple', yellow: false, sweet: false},
];

function addGroup(o){
  return {
    ...o,
    group: Object.keys(o).filter(key =&gt; o[key] === true).join(' ') 
  };
}

function addGroups(list){
  return list.map(o =&gt; addGroup(o));
}

console.log(addGroups(data))
.as-console-wrapper { max-height: 100% !important; top: 0; }