Javascript 使用.filter方法删除函数
我正在创建一个记事本应用程序,我的deleteNote功能被卡住了。我需要使用.filter方法删除带有指定id的便笺,并保留与该id不关联的便笺。这是我到目前为止的代码Javascript 使用.filter方法删除函数,javascript,node.js,api,require,Javascript,Node.js,Api,Require,我正在创建一个记事本应用程序,我的deleteNote功能被卡住了。我需要使用.filter方法删除带有指定id的便笺,并保留与该id不关联的便笺。这是我到目前为止的代码 const fs = require("fs"); const util = require("util"); // returns a unique ID for our returns const uuidv1 = require("uuid/v1"); /
const fs = require("fs");
const util = require("util");
// returns a unique ID for our returns
const uuidv1 = require("uuid/v1");
// creates a promified version of fs.readfile and writefile
const readFileAsync = util.promisify(fs.readfile);
const writeFileAsync = util.promisify(fs.writefile);
class Store {
read() {
return readFileAsync("./db/db.json", "utf8");
}
write(note) {
return writeFileAsync("./db/db.json", JSON.stringify(note));
}
getNotes() {
return this.read().then(notes => {
let parsedNotes;
try {parsedNotes = [].concat(JSON.parse(notes))}
catch (err) {
parsedNotes = [];
}
return parsedNotes;
})
}
// example of destructuring -- im taking the title, text string and destructuring it to add a unique id.
addNote(note) {
const {title, text} = note;
const newNote = {title, text, id: uuidv1()};
return this.getNotes()
.then(notes => [...notes, newNote]);
.then(updatedNotes => this.write(updatedNotes));
.then(() => newNote);
}
// delete note is going to take in an id and use a .filter to only keep the notes that do not include that id.
deleteNote() {
}
}
module.exports = new Store();
您已经知道正在进行筛选,因此只需执行以下操作: deleteNoteid{ 返回此文件。getNotes .thennotes=>notes.filternote=>note.id!==id .thenupdatedNotes=>this.writeupdatedNotes; }
您已经知道正在进行筛选,因此只需执行以下操作: deleteNoteid{ 返回此文件。getNotes .thennotes=>notes.filternote=>note.id!==id .thenupdatedNotes=>this.writeupdatedNotes; }
请尝试此操作,其中包括返回已删除的便笺 异步deleteNoteid{ const notes=wait this.getNotes; const filtered=notes.filternote=>note.id!==id; 等待这个。写过滤; const deleted=notes.filternote=>note.id==id; 返回已删除; }
请尝试此操作,其中包括返回已删除的便笺 异步deleteNoteid{ const notes=wait this.getNotes; const filtered=notes.filternote=>note.id!==id; 等待这个。写过滤; const deleted=notes.filternote=>note.id==id; 返回已删除; }