- Learning Node.js Development
- Andrew Mead
- 875字
- 2021-06-30 18:57:00
Using the removeNote function
The first step in the process is to fill out the removeNote function, which we defined in the previous chapters, and this will be your challenge. Let's remove console.log from the removeNote function in the notes.js file. You only need to write three lines of code to get this done.
Now, the first line will fetch the notes, then the job will be to filter out the notes, removing the one with title of argument. That means we want to go through all of the notes in the notes array, and if any of them have a title that matches the title we want to remove, we want to get rid of them. And this can be done using the notes.filter function we used earlier. All we have to do is switch the equality statement in the duplicateNotes function from equals to not equals, and this code will do just that.
It will go through the notes array. Every time it finds a note that doesn't match the title it will keep it, which is what we want, and if it does find the title it will return false and remove it from the array. And then we will add the third line, which is to save the new notes array:
var removeNote = (title) => {
// fetch notes
// filter notes, removing the one with title of argument
// save new notes array
};
The preceding code lines are the only three lines you need to fill out. Don't worry about returning anything from removeNote or filling out anything inside of app.js.
The first thing we will do for the fetchNotes line is to create a variable called notes, just like we did in addNote in the previous chapter, and we'll set it equal to the return result from fetchNotes:
var removeNote = (title) => {
var notes = fetchNotes();
// filter notes, removing the one with title of argument
// save new notes array
};
At this point our notes variable stores an array of all of the notes. The next thing we need to do is filter our notes.
If there is a note that has this title, we want to remove it. This will be done by creating a new variable, and I'll call this one filteredNotes. Here we'll set filteredNotes equal to the result that will come back from notes.filter, which we already used up previously:
var removeNote = (title) => {
var notes = fetchNotes();
// filter notes, removing the one with title of argument
var filteredNotes = notes.filter();
// save new notes array
};
We know that notes.filter takes a function as its one and only argument, and that function gets called with the individual item in the array. In this case it would be a note. And we can do this all on one line using the ES6 arrow syntax.
That means right here we can return true if note.title does not equal the title that's passed into the function:
var removeNote = (title) => {
var notes = fetchNotes();
var filteredNotes = notes.filter((note) => note.title !== title);
// save new notes array
};
This will populate filteredNotes with all of the notes whose titles do not match the one passed in. If the title does match the title passed in, it will not be added to filteredNotes because of our filter function.
The last thing to do is to call saveNotes. Right here, we'll call saveNotes passing in the new notes array which we have under the filteredNotes variable:
var removeNote = (title) => {
var notes = fetchNotes();
var filteredNotes = notes.filter((note) => note.title !== title);
saveNotes(filteredNotes);
// save new notes array
};
If we were to pass in notes, it wouldn't work as expected; we're filtering the notes out but we're not actually saving those notes, so it will not get removed from the JSON. We need to pass filteredNotes as shown in the preceding code. And we can test these by saving the file and trying to remove one of our notes.
I'll try to remove secret2 from the notes-data.json file. That means all we need to do is run the command, which we called remove, that is specified over in app.js, (refer to the following code image, and then it will call our function).
I'll run Node with app.js, and we'll pass in the remove command. The only argument we need to provide for remove is the title; there's no need to provide the body. I'll set this equal to secret2:
node app.js remove --title=secret2
As shown in the screenshot, if I hit enter you can see we don't get any output. Although we do have the command remove printing, there is no message saying whether or not a note was removed, but we'll add that later in the section.
For now, we can check the data. And right here you can see secret2 is nowhere in sight:
This means our remove method is indeed working as expected. It removed the note whose title matched and it kept all the notes whose title was not equal to secret2, exactly what we wanted.