Skip to content

Instantly share code, notes, and snippets.

@felixjb
Created January 9, 2024 14:40
Show Gist options
  • Save felixjb/c11b4851b2a92b93ef0e994b09539c8b to your computer and use it in GitHub Desktop.
Save felixjb/c11b4851b2a92b93ef0e994b09539c8b to your computer and use it in GitHub Desktop.
A collection of helpful tricks on JavaScript
// Create a Map from an Array
const array = [
{ key: "name", value: "Tom" },
{ key: "country", value: "Chile" },
];
// Return an array of tuples (array with two values, the key and the value)
const map = new Map(array.map((item) => [item.key, object.value]));
// ️👇️ {'name' => 'Tom', 'country' => 'Chile'}
console.log(map);
// Swap two variables in one line (XOR)
let [a, b] = [0, 1];
[a, b] = [b, a];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment