These are my notes for this video
Removing Falsy Values from an Array:
- Use the
filter
method with a callback function that returnsBoolean(name)
to remove falsy values.
- Use the
const filteredName = myName.filter(name => Boolean(name));
Checking for Even Numbers in an Array:
- Use the
some
method to check if at least one element in the array passes a test.
- Use the
const hasEvens = array.some(num => num % 2 === 0);
Removing Duplicate Items from an Array:
- Use a
Set
to automatically remove duplicate items and spread the set into a new array.
- Use a
const uniqueArray = [...new Set(array)];
Using Nullish Coalescing Operator for Default Values:
- Instead of using a ternary operator, use the nullish coalescing operator (
??
) to provide a default value for null or undefined.
- Instead of using a ternary operator, use the nullish coalescing operator (
const greeting = name ?? "friend";
Simplifying Sort Order in Array Sorting:
- Use
a
andz
as variables instead ofa
andb
when sorting in ascending order, and usez
anda
for descending order.
- Use
array.sort((a, z) => a - z); // ascending order
array.sort((z, a) => a - z); // descending order
Destructuring Arrays and Objects:
- Use destructuring assignment to assign variables from arrays or objects.
const [first, middle, last] = myName;
const { name, username, age } = person;
Creating Arrays of a Specific Length and Filling with Values:
- Use the
Array.from
method with a mapping function to create an array of a specific length filled with arbitrary values.
- Use the
const newArray = Array.from({ length: n }, (_, index) => index);
ย