DEV Community

Jan Küster 🔥
Jan Küster 🔥

Posted on

JavaScript puzzle oneliner - get unique values from list

Given the following datastructure:

const data = [{ foo: 'bar' }, { bar: 'baz' }, { foo: 'bar'}, { foo: 'baz'}, { foo: 'moo'}, { moo: 'bar'}, { foo: 'moo'}, { baz: 'moo' }, { foo: 'meh'}]
Enter fullscreen mode Exit fullscreen mode

Write a function getFoo that extracts all unique values for foo, except undefined. The overall code should be in one line using less than 60 characters.

Signature: object[] => string[]

No further conventions / restrictions. Think out of the box and not in clean code! Esoteric solutions welcomed!

Top comments (4)

Collapse
 
tae_sxtwilight profile image
Cleo G

const getFoo = d => d?.reduce((x, dd) => (dd?.hasOwnProperty('foo') && !x?.includes(dd?.foo)) ? [...x, dd.foo] : x, [])

Collapse
 
tae_sxtwilight profile image
Cleo G

Couldn't do it in less than 60 characters 😭😭

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

AWS Industries LIVE! Stream

Watch AWS Industries LIVE!

Discover how cloud technology is solving real-world problems on Industries LIVE!

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay