Posts

Showing posts from May, 2024

Mutate array by copy

  When working with arrays in JavaScript, it’s essential to understand the distinction between mutating an array in place and creating a new array by copying its elements. Let’s explore both approaches: Mutating the Original Array (In-Place) When you want to modify the original array directly, you can use methods that mutate the array without creating a new one. Examples of such methods include: Array#push() : Adds elements to the end of the array. Array#pop() : Removes the last element from the array. Array#splice() : Adds or removes elements at a specific position. Array#sort() : Sorts the array in place. Array#reverse() : Reverses the order of elements. These methods alter the original array, so use them carefully. Creating a New Array by Copying Elements Sometimes you need to create a new array based on the existing one without modifying the original. Common methods for creating a new array include: Array#map() : Creates a new array by applying a function to each element. Array...

Promise.withResolvers()

The   Promise.withResolvers()   method is a powerful addition to JavaScript’s Promise API. Let’s explore what it does and how you can use it: What Is  Promise.withResolvers() ? The  Promise.withResolvers()  static method returns an object containing: A new  Promise  object. Two functions:  resolve  and  reject . These functions correspond to the two parameters passed to the executor of the  Promise()  constructor. Syntax: JavaScript const { promise, resolve, reject } = Promise . withResolvers (); AI-generated code. Review and use carefully.  More info on FAQ . The  promise  is the newly created Promise. The  resolve  function resolves the promise (similar to the  resolve  function in the Promise constructor). The  reject  function rejects the promise (similar to the  reject  function in the Promise constructor). Example: Transforming a Stream to an Async Iterable Imagine y...

Regular expression v flag

  Certainly! The   v  flag   in JavaScript regular expressions is a relatively new addition that unlocks several powerful features related to Unicode properties of strings. Let’s explore what it does: What Is the  v  Flag? The  v  flag is an  “upgrade”  to the existing  u  flag (Unicode mode). It enables additional Unicode-related features beyond what the  u  flag provides. Unlike  u , which focuses on compatibility with existing ECMAScript behavior,  v  introduces new functionality. How to Use the  v  Flag: You can enable the  v  flag by adding it to your regular expression: JavaScript const re = /.../ v; AI-generated code. Review and use carefully.  More info on FAQ . Note that you  cannot combine  the  u  and  v  flags on the same regular expression. They are  completely separate modes . Features Unlocked by the  v  Flag: Unicode ...

JavaScript - Object.groupBy()

  Object.groupBy()   is a   powerful method   in JavaScript that allows you to group elements from an iterable (such as an array) based on the values returned by a specified callback function. Let’s dive into the details: What Does  Object.groupBy()  Do? The  Object.groupBy()  static method  groups elements  of a given iterable according to the  string values  returned by a provided callback function. The resulting object has separate properties for each group, each containing an array with the elements in that group. This method is particularly useful when group names can be represented by strings. Syntax: JavaScript Object . groupBy (items, callbackFn) AI-generated code. Review and use carefully.  More info on FAQ . items : An iterable (such as an array) whose elements will be grouped. callbackFn : A function executed for each element in the iterable. It should return a value that can be coerced into a  property key...