DEV Community

Cover image for What is your favorite one-liner?
Médéric Burlet
Médéric Burlet

Posted on

2 1

What is your favorite one-liner?

One thing that I love about JavaScript and especially es6 and onward is the possibility to do very cool one-liners.

small-code

One of my favorite is:

arrayOfObjects.find(e => e.name === 'my name' && ( e.description = 'your value', true ) );
Enter fullscreen mode Exit fullscreen mode

This let's us find an object in an Array of objects and edit its value.

example:

let arrayOfObjects = [
  {name: "my name", description: ''}, 
  {name: "another name", description: ''}, 
]
Enter fullscreen mode Exit fullscreen mode

by running the above one-liner the result would be:

let arrayOfObjects = [
  {name: "my name", description: 'your value'}, 
  {name: "another name", description: ''}, 
]
Enter fullscreen mode Exit fullscreen mode

If multiple objects fit the if condition they will all be changed.

question

What about you? What's your fav one-liner?


Burlet Mederic
https://mederic.me
https://twitter.com/crimson_med

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay