One-lines
Just a collection of JavaScript one-line tips.
Last updated
Just a collection of JavaScript one-line tips.
Last updated
const sortBy = (arr, key) => arr.sort((a, b) => a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0);const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);const countOccurrences = (arr, value) => arr.reduce((a, v) => (v === value ? a + 1 : a), 0);const wait = async (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));const pluck = (objs, key) => objs.map((obj) => obj[key]);const insert = (arr, index, newItem) => [...arr.slice(0, index), newItem, ...arr.slice(index)];