Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Dec 21 2018 17:11
    @MadLittleMods banned @OmarAlmaqtri
  • Jun 05 2018 23:13
    @MadLittleMods banned @LWSS
  • May 25 2018 16:50
    @MadLittleMods banned @TheRedstoneTaco
Julian Gonggrijp
@jgonggrijp

Oops, forgot that Object.create doesn't work exactly like _.create. You actually need to write this:

const john = Object.create(person, {
    firstName: {value: 'John', writable: true},
    lastName: {value: 'Doe', writable: true},
});

or this (using Underscore):

const john = _.create(person, {
    firstName: 'John',
    lastName: 'Doe',
});
This is a more interesting and useful way to use accessors:
const person = {
    get fullName() {
        return `${this.firstName} ${this.lastName}`;
    },
    set fullName(full) {
        const [first, last] = full.split(' ');
        this.firstName = first;
        this.lastName = last;
    },
};

const john = _.create(person, {
    firstName: 'John',
    lastName: 'Doe',
});

console.log(john.fullName); // 'John Doe'

john.fullName = 'Joe Dalton';

console.log(john.firstName); // 'Joe'
console.log(john.lastName); // 'Dalton'
Marco Colia
@justoverclockl
wow thats a good material for me to make practice!!! thx
Marco Colia
@justoverclockl
guys, i need to filter out an array of object, and i want to return only the item that contain one or more "done" flag === false, but i can't access the nested object...it return undefined
const notes = [
  {
    id: 1,
    description: 'Workout program',
    todos: [
      {
        id: 1,
        name: 'Push ups - 10 x 3',
        done: false
      },
      {
        id: 2,
        name: 'Abdominals - 20 x 3',
        done: true
      },
      {
        id: 3,
        name: 'Tapis Roulant - 15min',
        done: true
      }
    ]
  },
  {
    id: 2,
    description: 'Front-end Roadmap',
    todos: [
      {
        id: 1,
        name: 'Learn HTML',
        done: true
      },
      {
        id: 2,
        name: 'Learn CSS',
        done: true
      },
      {
        id: 3,
        name: 'Learn JavaScript',
        done: true
      },
      {
        id: 4,
        name: 'Learn Angular',
        done: true
      }
    ]
  }
]

const filt = notes.filter((el) =>{
  console.log(el.todos.done) // undefined
})

/* expected output

{
    id: 1,
    description: 'Workout program',
    todos: [
      {
        id: 1,
        name: 'Push ups - 10 x 3',
        done: false                                        //this contain a false flag...so this object need to be returned
      },
      {
        id: 2,
        name: 'Abdominals - 20 x 3',
        done: true
      },
      {
        id: 3,
        name: 'Tapis Roulant - 15min',
        done: true
      }
    ]
  } */
seems that is possible with filter method and antoher method that is not map
Stephen James
@sjames1958gm

el.todos.done todos is an array and doesn't have a done property. You need to use another function on the todos array.
You can use the .any function to return true if any value in the array passes the function.
array.any(fn) where fn is passed the elements and if it returns true for any element .any return true.

notes.filter(note => notes.todos.any(todo => todo.done))

Gianfabio Pezzolla
@gianfabiopezzolla
@sjames1958gm .any is used in ruby, in js you need to use some
Gianfabio Pezzolla
@gianfabiopezzolla
@justoverclockl you can reach your goal with the following way:
const notes = [
  {
    id: 1,
    description: 'Workout program',
    todos: [
      {
        id: 1,
        name: 'Push ups - 10 x 3',
        done: false
      },
      {
        id: 2,
        name: 'Abdominals - 20 x 3',
        done: true
      },
      {
        id: 3,
        name: 'Tapis Roulant - 15min',
        done: true
      }
    ]
  },
  {
    id: 2,
    description: 'Front-end Roadmap',
    todos: [
      {
        id: 1,
        name: 'Learn HTML',
        done: true
      },
      {
        id: 2,
        name: 'Learn CSS',
        done: true
      },
      {
        id: 3,
        name: 'Learn JavaScript',
        done: true
      },
      {
        id: 4,
        name: 'Learn Angular',
        done: true
      }
    ]
  }
]

const hasFalseDone = notes.filter(note => note.todos.some(todo => todo.done === false))

/**

[[object Object] {
  description: "Workout program",
  id: 1,
  todos: [[object Object] {
  done: false,
  id: 1,
  name: "Push ups - 10 x 3"
}, [object Object] {
  done: true,
  id: 2,
  name: "Abdominals - 20 x 3"
}, [object Object] {
  done: true,
  id: 3,
  name: "Tapis Roulant - 15min"
}]
}]

**/
Julian Gonggrijp
@jgonggrijp
To be fair, with Underscore you can call it either any or some. And write things a bit more shortly.
_.filter(notes, note => _.some(note.todos, {done: false}))
// equivalent:
_.filter(notes, note => _.any(note.todos, {done: false}))
Marco Colia
@justoverclockl
can someone explain this in detail? so...what is the first parameter of notes.filter? and why the arrow?
and what is .some (i knnow that is a method) but what it return
Marco Colia
@justoverclockl
@gianfabiopezzolla your code return
[
  {
    id: 1,
    description: 'Workout program',
    todos: [ [Object], [Object], [Object] ]
  }
]
that is not the expected result @jgonggrijp
expected result must be:
{
    id: 1,
    description: 'Workout program',
    todos: [
      {
        id: 1,
        name: 'Push ups - 10 x 3',
        done: false                                        //this contain a false flag...so this object need to be returned
      },
      {
        id: 2,
        name: 'Abdominals - 20 x 3',
        done: true
      },
      {
        id: 3,
        name: 'Tapis Roulant - 15min',
        done: true
      }
    ]
  }
Julian Gonggrijp
@jgonggrijp
@justoverclockl That's the same result, the items in the todos array have just been collapsed in your first quote.
19 replies
Stephen James
@sjames1958gm
My example was wrong as I was testing done === true, not done === false. Also, I forgot that for js it is "some" not "any".
Julian Gonggrijp
@jgonggrijp

can someone explain this in detail? so...what is the first parameter of notes.filter?

A function that returns a boolean. filter returns all elements of the array (or more generally, of the collection if you use _.filter) for which that function returns true.

and why the arrow?

That's basically a shorthand for an anonymous function. (a, b) => c means roughly (not exactly) the same as function(a, b) { return c; }.

Stephen James
@sjames1958gm
@gianfabiopezzolla That's what I get for using my memory :(
Julian Gonggrijp
@jgonggrijp

and what is .some (i knnow that is a method) but what it return

It returns a boolean: true if at least one element in the array/collection passes the test, false otherwise.

Marco Colia
@justoverclockl
guys, how do you override a method in an extended class? i have a class that returns a bank account deposit, in an extended class i want to return the same values plus 3% more if the deposit is >= 1000

class BankAccount {
  #amount = 0;

  constructor(initialAmount) {
    this.#amount = initialAmount;
  }

  deposit(amount) {
    this.#amount += amount;
  }

  withdraw(amount) {
    this.#amount -= amount;
  }

  view() {
    console.log(this.#amount);
  }
}

class BankAccountVip extends BankAccount {                 //    i've started to create this
  constructor(initialAmount, interest) {
    super(initialAmount);
  }
}


const bankAccountVip = new BankAccountVip(1000);
bankAccountVip.deposit(500);
bankAccountVip.deposit(1200);
bankAccountVip.withdraw(800);
bankAccountVip.deposit(3500);
bankAccountVip.view();          // this need to be returned with 3% of interests if the balance is >= 1000
Marco Colia
@justoverclockl
i need to override the deposit method maybe...but how
Stephen James
@sjames1958gm
Marco Colia
@justoverclockl
tried with something like

class BankAccountVip extends BankAccount {
  constructor(initialAmount, interest) {
    super(initialAmount);
    this.interest = interest
  }
  deposit(amount) {
    super.deposit(amount);
    if (amount >= 1000) {
      console.log(amount)
      return amount + (amount *= 3 / 100)
    }
  }
}
but not work, the view(parent) does not add the percentage
Stephen James
@sjames1958gm
Your overriding deposit function is not updating the #amount variable
Maybe call super.deposit(amount); with the updated amount if >= 1000
Marco Colia
@justoverclockl
i'm trying with no success
Stephen James
@sjames1958gm
deposit(amount) {

    if (amount >= 1000) {
      console.log(amount)
      amount += (amount * (3 / 100))
    }
    super.deposit(amount);
  }
Marco Colia
@justoverclockl
damn.........3 hour to understand this
Stephen James
@sjames1958gm
It'll get better over time and then you'll spend 3 hours on some stupid typo, which you are just not seeing.
Marco Colia
@justoverclockl
:D
Marco Colia
@justoverclockl
what i need to be clear is if i can do try catch inside a class method, for example if i want to check if deposit is not a negative number
because if i do try catch inside deposit(amount) basically nothing happen...all run normally without any check
Marco Colia
@justoverclockl
this work

deposit(amount) {
    if (amount <= 0) {
      throw 'Error: value must be  > than 0'
    }
      this.#amount += amount;

  }
but not with try catch
Marco Colia
@justoverclockl
this work also
deposit(amount) {
    try {
      if (amount <= 0) {
        throw new Error('Error: value must be  > than 0')
      }
    }
    catch(err) {
      console.log(err.message)
    }
    finally {
      this.#amount += amount;
    }
  }
but if i do bankAccount.deposit(-1) i see the error but the deposit is executed
Marco Colia
@justoverclockl
maybe i need to remove finally...then it works
Marco Colia
@justoverclockl
i hope i found the correct way
Stephen James
@sjames1958gm

The try/catch will hide the exception from outside the function.
Code that throws and catches in the same location doesn't really make sense.

try {
   throw "x"
} catch "x" {
}

The finally block says after trying or catching do this, so that is not what you would want.

The first way seems correct and then you would have the try/catch where you are calling deposit
Marco Colia
@justoverclockl
guys there's a way to filter out only id and age entries from an object?

const person = {
  id: 1,
  firstName: 'john',
  lastName: 'doe',
  age: 25,
};

const json = JSON.stringify(person);

console.log(json); // Should return: { id: 1, age: 25 }

// output is: {"id":1,"firstName":"john","lastName":"doe","age":25}
Marco Colia
@justoverclockl
nevermind, i've figured it out const json = JSON.stringify(person,['id','age']);
Niko
@niko:chat.tgp.io
[m]
if you dont want to stringify it: const { id, age } = person; const filteredPerson = { id, age };
Julian Gonggrijp
@jgonggrijp
Or _.pick(person, ['id', 'age']).
Marco Colia
@justoverclockl
yeah, now that is clear, now the problem is the next exercise, object destructuring -.-
Stephen James
@sjames1958gm
What Niko did above is exactly that. With {} on the left of the = your are selecting fields from the object and assigning them to like named variables
Marco Colia
@justoverclockl
yes but what if i want to destructure the above person object and return a key: value using only spread/rest operator?
if i do: const { id, firstName, lastName, age } = person i get only value
so the output is 1 John Doe 25