create account

RE: Removing a Note: Day Seven of "The Complete Node.js Developer Course" by petarjs

View this thread on: hive.blogpeakd.comecency.com

Viewing a response to: @matthewdavid/removing-a-note-day-seven-of-the-complete-node-js-developer-course

· @petarjs ·
$0.05
Hi Matthew! It's really inspiring to see how you're going about this. Initiative and passion are the key.

I'd like to give my take on explaining this line:

```
var rmDuplicate = notes.filter((note) => note.title !== title)
```

First of all, lets call the variable `uniqueNotes` to better describe what it is, as we're removing the duplicate notes so we can save only the unique ones:

```
const uniqueNotes = notes.filter((note) => note.title !== title)
```

To better understand what it does, let's forget about arrow functions for a moment, so we can write it as an ordinary function and work our way to the arrow function notation.

```
const uniqueNotes = notes.filter(function (note) {
    return note.title !== title
})
```

So far so good. As a note, the `filter` function can be called on any array in JavaScript. You pass it a function as an argument , which is invoked for every element. This predicate function returns `true` if the element should stay or `false` if we want to remove it. Then, the `filter` function returns a new array comprised of all the elements that we decided should stay.

Ok, now we can refactor our predicate function to be an arrow function:

```
const uniqueNotes = notes.filter((note) => {
    return note.title !== title
})
```

Pretty similar, right? But we can go further, and remove the parenthesis around the `note` argument (because there is only one argument, if there were multiple, we'd have to keep the parenthesis)

```
const uniqueNotes = notes.filter(note => {
    return note.title !== title
})
``` 

Okay! But we can make it even more compact by removing the curly braces that denote the function block (we can do this only if our arrow function has one line, and this one does)

```
const uniqueNotes = notes.filter(note => note.title !== title)
```

By removing the curly braces, we also need to remove the `return` keyword, as it is implied when using this shorthand arrow function notation.

So what's the difference between using an arrow function and a regular function?

Well, obviously it's more expressive, meaning that it tells you what it's doing using less characters like curly brackets and the `return` statement.

But there's another thing that makes the arrow functions different - their scope. Arrow functions don't have their own scope, but are bound to the scope of the function that they are defined in.

In practice, this means that `this` inside of the arrow function will be that of the function they are called inside of. You'll find this quite useful if you've ever had to use the old `var that = this` trick to access the context of the parent function. Now you can simply use arrow functions!
👍  
properties (23)
authorpetarjs
permlinkre-matthewdavid-removing-a-note-day-seven-of-the-complete-node-js-developer-course-20170915t230624886z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-15 23:06:24
last_update2017-09-15 23:06:24
depth1
children1
last_payout2017-09-22 23:06:24
cashout_time1969-12-31 23:59:59
total_payout_value0.035 HBD
curator_payout_value0.011 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,677
author_reputation1,710,016,701,366
root_title"Removing a Note: Day Seven of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id15,008,681
net_rshares17,730,650,968
author_curate_reward""
vote details (1)
@matthewdavid ·
Thank you! 

This was very helpful. It was good to see it re-phrased with familiar javascript syntax and then refactored step by step to using an arrow function. I really appreciate your detailed comment.
properties (22)
authormatthewdavid
permlinkre-petarjs-re-matthewdavid-removing-a-note-day-seven-of-the-complete-node-js-developer-course-20170915t235217261z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-15 23:52:18
last_update2017-09-15 23:52:18
depth2
children0
last_payout2017-09-22 23:52:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length204
author_reputation2,155,255,892,877
root_title"Removing a Note: Day Seven of "The Complete Node.js Developer Course""
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id15,010,859
net_rshares0