Skip to content

Instantly share code, notes, and snippets.

@BiosBoy
Created August 12, 2024 20:39
Show Gist options
  • Save BiosBoy/08acf7c214d9d4877d0ebf0294109b02 to your computer and use it in GitHub Desktop.
Save BiosBoy/08acf7c214d9d4877d0ebf0294109b02 to your computer and use it in GitHub Desktop.
const arr1 = [1, 2, 3, 4];
const result1 = arr1.flatMap(x => [x, x * 2]);
console.log(result1); // [1, 2, 2, 4, 3, 6, 4, 8]
const arr2 = ['a', 'b', 'c', 'd'];
console.log(arr2.at(-1)); // 'd'
console.log(arr2.at(-2)); // 'c'
const arr3 = [1, 2, 3, 4, 5];
const result2 = arr3.findLast(x => x % 2 === 0);
console.log(result2); // 4
const people = [
{ name: 'Alice', age: 21 },
{ name: 'Bob', age: 21 },
{ name: 'Charlie', age: 25 },
];
const groupedByAge = people.groupBy(person => person.age);
console.log(groupedByAge);
/*
{
21: [
{ name: 'Alice', age: 21 },
{ name: 'Bob', age: 21 }
],
25: [
{ name: 'Charlie', age: 25 }
]
}
*/
const arr4 = [3, 1, 4, 2];
const sorted = arr4.toSorted((a, b) => a - b);
console.log(arr4); // [3, 1, 4, 2]
console.log(sorted); // [1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment