Skip to main content
deleted 3 characters in body
Source Link
Álvaro
  • 187
  • 1
  • 9

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}) => { const res = Object.entries(rest).reduce((total, [key, value]) => { if (value === true) total.push(key) return total }, []) acc.push(res) return acc }, []).map(entry => ({ combined: entry.join(' ') })) const output = data.map((entry, index) => ({ ...entry, ...combined[index] })) console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }

It seems pretty straight forward but somehow this feels is a bit convoluted

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}) => { const res = Object.entries(rest).reduce((total, [key, value]) => { if (value === true) total.push(key) return total }, []) acc.push(res) return acc }, []).map(entry => ({ combined: entry.join(' ') })) const output = data.map((entry, index) => ({ ...entry, ...combined[index] })) console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }

It seems pretty straight forward but somehow this feels is a bit convoluted

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}) => { const res = Object.entries(rest).reduce((total, [key, value]) => { if (value === true) total.push(key) return total }, []) acc.push(res) return acc }, []).map(entry => ({ combined: entry.join(' ') })) const output = data.map((entry, index) => ({ ...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

Source Link
Álvaro
  • 187
  • 1
  • 9

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}) => { const res = Object.entries(rest).reduce((total, [key, value]) => { if (value === true) total.push(key) return total }, []) acc.push(res) return acc }, []).map(entry => ({ combined: entry.join(' ') })) const output = data.map((entry, index) => ({ ...entry, ...combined[index] })) console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }

It seems pretty straight forward but somehow this feels is a bit convoluted