简体   繁体   English

如何检查这个 function 是否为真,或者如果为假则返回一个空数组?

[英]How can I check this function for truthy or return an empty array if falsy?

I am looking at this assignment, and my question is about the part in bold at the end:我正在看这个作业,我的问题是关于最后加粗的部分:

 // Do not edit the code below. var myGroceryList = ['chips', 'pizza', 'hotpockets', 'MtnDew', 'corndogs']; // Do not edit the code above.

Here we're going to write a function that mimics going shopping and checking things off of our grocery list and adding new items to our list.在这里,我们将编写一个 function 来模拟购物和检查我们的杂货清单上的东西并将新项目添加到我们的清单中。

Write a function called removeItem that is given two arguments, the first is myGroceryList , and the second is an item to remove from myGroceryList .编写一个名为 removeItem 的 function ,给定两个 arguments,第一个是myGroceryList ,第二个是要从myGroceryList中删除的项目。 If the second argument (or the item to add or remove) matches an item in myGroceryList , remove that item from the your grocery list and return the new, updated grocery list.如果第二个参数(或要添加或删除的项目)与myGroceryList中的项目匹配,则从您的购物清单中删除该项目并返回新的、更新的购物清单。

Once you do that, write another function called addItem that is given two arguments, the first is myGroceryList and the second is an item to add to your grocery list.完成此操作后,编写另一个名为addItem的 function 并给出两个 arguments,第一个是myGroceryList ,第二个是要添加到您的购物清单的项目。 In addItem add the item you passed in to myGroceryList then return the new, updated grocery list.addItem中,将您传入的项目添加到myGroceryList ,然后返回新的、更新的购物清单。

In both removeItem and addItem check to see if the 'myGroceryList' and 'item' arguments are truthy.在 removeItem 和 addItem 中检查“myGroceryList”和“item”arguments 是否真实。 If they are not, return an empty array.如果不是,则返回一个空数组。

Here are some examples of calling your functions and what should be returned:以下是调用函数的一些示例以及应返回的内容:

 removeItem(myGroceryList, 'chips') --> ['pizza', 'hotpockets', 'MtnDew', 'corndogs']; addItem(myGroceryList, 'Jerky') --> ['pizza', 'hotpockets', 'MtnDew', 'corndogs', 'Jerky']; removeItem(myGroceryList) --> []; addItem() --> [];

Here is my code:这是我的代码:

removeItem=(myGroceryList,item)=>{
   return myGroceryList.filter((thing)=>{
      return thing != item
   })
}

addItem=(myGroceryList, item)=>{
   myGroceryList.push(item);
   return myGroceryList;
} 

How can I get the final step of this to work?我怎样才能让这最后一步起作用?

Simply add an if that verifies the truthiness of both arguments:只需添加一个if即可验证 arguments 的真实性:

removeItem=(myGroceryList,item)=>{
   if (!myGroceryList || !item) return [];
   return myGroceryList.filter((thing)=>{
      return thing != item
   })
}

Not mutating the argument不改变论点

As you are asked to return the new list, you did well to use filter and not mutate the original list.当您被要求返回新列表时,您很好地使用了filter并且没有改变原始列表。 But you should better apply the same principle to addItem , which also is supposed to return the new list, and so it is better not to mutate the list that was given:但是您最好将相同的原则应用于addItem ,它也应该返回新列表,因此最好不要改变给出的列表:

addItem=(myGroceryList, item)=>{
   if (!myGroceryList || !item) return [];
   return myGroceryList.concat(item); // don't use push, but concat
} 

Or... mutating the argument?或者……改变论点?

However, the example code that is given at the end of the assignment, does not use the return value of the function (except the console reporting it), and from the second call we can see that "chips" has actually been removed from the list that was given to the first call of removeItem .然而,在分配结束时给出的示例代码,没有使用 function 的返回值(除了控制台报告它),从第二个调用中我们可以看到“芯片”实际上已经从第一次调用removeItem列表。

So that means you have to make your functions having side-effects .所以这意味着你必须让你的函数有副作用

In that case you should not use filter or concat , but change the first function to use splice :在这种情况下,您不应该使用filterconcat ,而是将第一个 function 更改为使用splice

removeItem=(myGroceryList,item)=>{
   if (!myGroceryList || !item) return [];
   let i = myGroceryList.indexOf(item);
   if (i >= 0) myGroceryList.splice(i, 1); // mutate the list
   return myGroceryList;
}

...and you would use your version with push for addItem : ...并且您可以将您的版本与push for addItem一起使用:

addItem=(myGroceryList, item)=>{
   if (!myGroceryList || !item) return [];
   myGroceryList.push(item); // mutate...
   return myGroceryList;
} 

Instead of checking if truthy, you can check if falsy and return an empty array right away.您可以检查是否为假并立即返回一个空数组,而不是检查是否为真。

   addItem=(myGroceryList, item) => {
      if(!myGroceryList) { return [] }
      myGroceryList.push(item);
      return myGroceryList;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM