【问题标题】:Node.Js removing one item from an array using the .filter methodNode.Js 使用 .filter 方法从数组中删除一项
【发布时间】:2019-01-20 11:18:07
【问题描述】:

我有一个对象数组,我正在尝试根据用户传递的参数删除特定项目,

removeItem = (title, body){

let myArray = [
  { title : 'title 1', body: 'body of title one' },
  { title : 'title 2', body: 'body of title two' },
  { title : 'title 3', body: 'body of title three' },
]

//the problem is down here
let filteredArray = myArray.filter(item => {
  item.title != title || item.body != body
}
  
// at this point i assume that the filtered array will not
// include the item that i want to remove
// so down here i reset the value of my original array to the filtered one
 
myArray = filteredArray

简单来说,我要运行的测试是, 如果用户的标题或正文与数组中的标题或正文不匹配,则将此项目放入一个新数组中,因此我将有一个过滤数组..

然而,发生的事情是,上面的代码删除了一切并返回一个空数组。 有人可以帮助纠正上面的逻辑吗? 提前致谢

【问题讨论】:

  • 过滤函数需要返回一个布尔值,如果你想“保持”它为true,则将其删除。所以你的代码应该是return item.title != title || item.body != body
  • 代码没有运行

标签: javascript arrays node.js


【解决方案1】:

从箭头函数中添加 return 语句或 删除 大括号:)

myArray = myArray.filter(item => {
  return item.title != title || item.body != body;
});

myArray = myArray.filter(item => item.title != title || item.body != body);

【讨论】:

    【解决方案2】:

    您需要将boolean 值返回给您的过滤器函数。返回true 以保留元素,否则返回false。 当前您返回的是未定义的,这是错误的。

    let myArray = [
      {title : 'title 1', body: 'body of title one'},
      {title : 'title 2', body: 'body of title two'},
      {title : 'title 3', body: 'body of title three'}
    ]
    let title = "title 1";
    let body = "body of title one";
    
    
    let filteredArray = myArray.filter((item) =>item.title != title || item.body != body);
    
     console.log(filteredArray);

    供参考:Array.filter()

    【讨论】:

      【解决方案3】:

      From MDN Filter page

      回调 函数是一个谓词,用来测试数组的每个元素。返回 true 以保留元素,否则返回 false。它接受三个参数:

      您的回调函数没有任何return 关键字。默认情况下,JS 会将此解释为undefined,即非真实/虚假值。因此,您的所有项目都无法匹配过滤条件,并且您将得到一个空数组。

      另外,正如其他人指出的那样,您缺少括号。

      let filteredArray = myArray.filter(item => {
          return item.title != title || item.body != body;
      });
      

      【讨论】:

      • 哇 :D,实际上我对你们所有人的快速响应感到非常惊讶 :) 非常感谢,您的回答确实解决了问题,非常感谢 ;) 干杯
      【解决方案4】:

      如果您想过滤 either 正文 标题 not 匹配的情况,您实际上需要使用 && in你是布尔值。

      例如,如果您的 title 是“标题 1”,而您的 body 是“标题 2 的正文”,则它应该返回的唯一项目是 title 3,因为这是唯一一个两者都没有 em> 标题和正文匹配。

      当然,您需要从过滤器回调中返回此结果,因此要么删除函数体周围的{},要么显式返回值。

      const removeItem = (title, body) => {
        let myArray = [
          {title : 'title 1', body: 'body of title one'},
          {title : 'title 2', body: 'body of title two'},
          {title : 'title 3', body: 'body of title three'}
        ]
        
        let filteredArray = myArray.filter((item) =>
          item.title != title 
          && item.body != body
        );
        return filteredArray  
      }
      
       console.log(removeItem( // should only return title 3
        "title 1",
        "body of title two"
       ));
      
       console.log(removeItem( //should return title 2 and title 3
        "title 1",
        "body of title one"
       ));

      【讨论】:

        猜你喜欢
        • 2019-07-04
        • 1970-01-01
        • 2019-11-10
        • 2020-02-16
        • 1970-01-01
        • 2014-01-24
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        相关资源
        最近更新 更多