我的函数应该删除一个配方。
所以我只在ID不等于传递给我的ID的情况下才会过滤所有的配方。
(只有在数组上找到ID时,我们才应该删除配方)
IDs是使用软件包uuid(v4方法)生成的。
const removeRecipe = (id) => {
const recipes = loadRecipes();
const recipesToKeep = recipes.filter((recipe) => {
return recipe.id !== id;
});
if (recipes.legth > recipesToKeep.legth) {
console.log(chalk.green.inverse('Recipe Removed'));
saveRecipes(recipesToKeep);
} else {
console.log(chalk.red.inverse('Recipe not found'));
}
};
问题是,即使ID值和type(string)都相同,过滤函数应该忽略配方,但函数仍然返回false。
所以 "c3a76b5b-bc36-4e87-8e2c-229211ca265d" === "c3a76b5b-bc36-4e87-8e2c-229211ca265d" 是真,不应该是过滤数组的一部分,但它被评估为假。
我有一个错别字
if (recipes.legth > recipesToKeep.legth) {
我必须是
if (recipes.length > recipesToKeep.length) {