Arrays In Javascript
- Can convert an object to an array of pairs with
Object.entries()
- Check if item is in array:
const sections = ['contact', 'shipping'];
function displayShipping(sections) {
return sections.includes('shipping');
}
console.log(displayShipping(sections));
Spread Operator
- Remove item from array (not modifying the original array) with the spread operator:
function removeItem(items, removable) {
const index = items.indexOf(removable);
return [...items.slice(0, index), ...items.slice(index + 1)];
}
- Use a provided array in a function and use it, while keeping the function pure:
function addGift(cart) {
if (cart.length > 2) {
return [...cart, reward];
}
return cart;
}
- Instead of sorting an array and mutating it, sort a copy of the array:
Created from: Modern Javascript Course 202006070113
uid: 202006091927 tags: #programming #javascript