# One-lines

* ### How to Capitalize Text <a href="#id-1-how-to-capitalize-text" id="id-1-how-to-capitalize-text"></a>

  ```js
  const capitalize = (str) => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
  ```

* ### How to Calculate Percent <a href="#id-2-how-to-calculate-percent" id="id-2-how-to-calculate-percent"></a>

  ```js
  const calculatePercent = (value, total) => Math.round((value / total) * 100)
  ```

* ### How to Get a Random Element <a href="#id-3-how-to-get-a-random-element" id="id-3-how-to-get-a-random-element"></a>

  ```js
  const getRandomItem = (items) =>  items[Math.floor(Math.random() * items.length)];
  ```

* ### How to Remove Duplicate Elements <a href="#id-4-how-to-remove-duplicate-elements" id="id-4-how-to-remove-duplicate-elements"></a>

  ```js
  const removeDuplicates = (arr) => [...new Set(arr)];
  ```

* ### How to Sort Elements By Certain Property <a href="#id-5-how-to-sort-elements-by-certain-property" id="id-5-how-to-sort-elements-by-certain-property"></a>

  ```js
  const sortBy = (arr, key) => arr.sort((a, b) => a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0);
  ```

* ### How to Check if Arrays/Objects are Equal <a href="#id-6-how-to-check-if-arrays-objects-are-equal" id="id-6-how-to-check-if-arrays-objects-are-equal"></a>

  ```js
  const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
  ```

* ### How to Count Number of Occurrences <a href="#id-7-how-to-count-number-of-occurrences" id="id-7-how-to-count-number-of-occurrences"></a>

  ```js
  const countOccurrences = (arr, value) => arr.reduce((a, v) => (v === value ? a + 1 : a), 0);
  ```

* ### How to Wait for a Certain Amount of Time <a href="#id-8-how-to-wait-for-a-certain-amount-of-time" id="id-8-how-to-wait-for-a-certain-amount-of-time"></a>

  ```js
  const wait = async (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
  ```

* ### How to Use the Pluck Property from Array of Objects <a href="#id-9-how-to-use-the-pluck-property-from-array-of-objects" id="id-9-how-to-use-the-pluck-property-from-array-of-objects"></a>

  ```js
  const pluck = (objs, key) => objs.map((obj) => obj[key]);
  ```

* ### How to Insert an Element at a Certain Position <a href="#id-10-how-to-insert-an-element-at-a-certain-position" id="id-10-how-to-insert-an-element-at-a-certain-position"></a>

  ```js
  const insert = (arr, index, newItem) => [...arr.slice(0, index), newItem, ...arr.slice(index)];
  ```

***

> Reference

<https://www.freecodecamp.org/news/javascript-one-liners-to-use-in-every-project/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://thejb.gitbook.io/core/programming/js/one-lines.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
