Skip to content

Instantly share code, notes, and snippets.

@lucafregoso
Last active April 20, 2023 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucafregoso/e305eb20f0dad9296dc19a6284e70341 to your computer and use it in GitHub Desktop.
Save lucafregoso/e305eb20f0dad9296dc19a6284e70341 to your computer and use it in GitHub Desktop.
One Liners

Capitalize Text

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

Calculate Percent

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

Get a Random Element

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

Remove Duplicate Elements from Array

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

Sort Elements By Certain Property

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

Check if Arrays/Objects are Equal

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

Count Number of Occurrences

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

Wait for a Certain Amount of Time

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

	wait(2000).then(() => goToSignupPage());

Use the Pluck Property from Array of Objects

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


    const users = [{ name: "Abe", age: 45 }, { name: "Jennifer", age: 27 }];
    pluck(users, 'name'); // ['Abe', 'Jennifer']

Insert an Element at a Certain Position

    const insert = (arr, index, newItem) => [...arr.slice(0, index), newItem, ...arr.slice(index)];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment