create account

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

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

· @woz.software ·
$0.05
Do you need this part?

var note = {title} ?

JS is dynamic so not sure you need the definition. If not you could reduce to this :)

```
var removeNote = (title) => {
    saveNotes(fetchNotes().filter((note) => note.title !== title)
}
```

The bit you are fighting with should click in a minute...

```
var wantNote = (note) => note.title !== title;
```

Notice how similar the definition of removeNote is when written this way. That is because wantNote is just another function but in this case you have not assigned it to a variable, just passed it direct to filter.

Filter is known as a high order function, in that it takes a function as an argument. You can pass functions around as you would say a string. They are just another bit of data.

This is very common in JS and also many current languages. 

How filter works is this, it gets the list of notes and your tester function. It then walks through all the notes and tests if you want to keep it, if so it adds it to the results and if not it discards it. The results are all the notes that passed the test function you supplied.

Done in C# as know the syntax better but similar enough you should understand :)

There is a cleaner way but this should make it easy to see what is going on

```
public static IEnumerable<Note> Filter(
    this IEnumerable<Note> toFilter, Func<Note, bool> predicate)
{
    var result = new List<Note>();
    foreach (var note in toFilter)
    {
        if (predicate(note))
        {
            result.Add(note);
        }
    }
    return result;
}
```

So this gets an IEnumerable (list) of notes and a function from note to bool. Called predicate as that is the common term for something that takes a value and returns true/false because maths :)

HTH

Woz
👍  
properties (23)
authorwoz.software
permlinkre-matthewdavid-removing-a-note-day-seven-of-the-complete-node-js-developer-course-20170912t064326670z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-12 06:43:30
last_update2017-09-12 06:43:30
depth1
children2
last_payout2017-09-19 06:43:30
cashout_time1969-12-31 23:59:59
total_payout_value0.038 HBD
curator_payout_value0.013 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,753
author_reputation2,321,910,395,519
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_id14,629,234
net_rshares18,863,291,227
author_curate_reward""
vote details (1)
@matthewdavid ·
Thank you. This helps!

I appreciate that you took the time to explain it.
👍  
properties (23)
authormatthewdavid
permlinkre-wozsoftware-re-matthewdavid-removing-a-note-day-seven-of-the-complete-node-js-developer-course-20170913t042749915z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-13 04:27:54
last_update2017-09-13 04:27:54
depth2
children1
last_payout2017-09-20 04:27:54
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_length74
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_id14,723,475
net_rshares6,514,013,641
author_curate_reward""
vote details (1)
@woz.software ·
$0.05
More than happy to help. You are doing the right thing writing like this, it helps get things clear in your head. I found that with my roguelike series. Actually found bugs and fixed them while writing the articles explaining the code :)
👍  
properties (23)
authorwoz.software
permlinkre-matthewdavid-re-wozsoftware-re-matthewdavid-removing-a-note-day-seven-of-the-complete-node-js-developer-course-20170913t095509874z
categoryjavascript
json_metadata{"tags":["javascript"],"app":"steemit/0.1"}
created2017-09-13 09:55:09
last_update2017-09-13 09:55:09
depth3
children0
last_payout2017-09-20 09:55:09
cashout_time1969-12-31 23:59:59
total_payout_value0.038 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length237
author_reputation2,321,910,395,519
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_id14,744,987
net_rshares18,567,009,166
author_curate_reward""
vote details (1)