Skip to content

Instantly share code, notes, and snippets.

View sandrabosk's full-sized avatar
👩‍💻
Always a student, never a master. Have to keep moving forward. ~C.Hall

Aleksandra Bošković sandrabosk

👩‍💻
Always a student, never a master. Have to keep moving forward. ~C.Hall
  • Ironhack
View GitHub Profile

The most common ways of iterating over arrays

for loop

Probably the most used way of iterationg through arrays so far:

const names = ["carlos", "ana", "alex"];

for(let i = 0; i < names.length; i++){
  1. allowedToDrink will be the list of people who are older than 21.
const allowedToDrink = people.filter(person =>{
  return person.age >= 21;
});
// in one line:
// const allowedToDrink = people.filter(person => person.age >= 21);
console.log(allowedToDrink);

// [ { name: 'Candice', age: 25 },
const capsMs = cities1.filter(theCity => theCity[0] === "m")
                      .map(city => {
                        if (city.includes(" ")){
                          let splitted = city.split(" ");
                          const len = splitted.length;
                          for (let i = 0; i < len; i++){
                            splitted[i] = splitted[i][0].toUpperCase() + splitted[i].slice(1);
                          }
 return splitted.join(" ");
const newArray = numbers.filter(number =>{
  return ((number % 2 ===1)&&(number<100))
});
console.log(newArray); // [ 1, 99, 73 ]

.reverse()

  • mutates the original array so needs to be cloned before applying .reverse() on it
  • returns the array of the same size in reversed order

Source

We will be working on the following array:

VS Code shortcuts

  • Search text through all files at once

    • Mac: Command + Shift + f
    • Windows: Ctrl + Shift + f
  • Re-open a closed editor When you have multiple open files and accidentally close one or more of them, finding them, and re-opening can be time-consuming and a little frustrating. This is an undo feature exclusively for tabs:

  • Mac: Command + Shift + t

// Example:
// User has an array of references to posts
// Posts have an array of references to comments
// Or Dani's example: https://github.com/webmad-shared/doublepopulate/blob/master/bin/seeds.js
router.get('/posts', (req, res, next) => {
Model.find()
.populate('oneProperty') // .populate("creator")
.populate({
// example with Alina Z.
Act.find()
.then(allSuggestedActs => {
const user = new User({
fullName,
email,
encryptedPassword,
score: 0,
const product = {
name: "AmazonBasics Apple Certified Lightning to USB Cable",
price: 7.99,
manufacter: "Amazon",
reviews:
[
{ user: "Pavel Nedved",
comments: "It was really usefull, strongly recommended",
rate: 4
},

How to connect local and remote repos?

(will be demonstrated on the example of creating "Katas" folder locally and on the GitHub )

locally (on your laptop)

$ mkdir katas
$ cd katas
$ git init # initialize Git repo