【问题标题】:Javascript filter data based on multiple fieldsJavascript基于多个字段过滤数据
【发布时间】:2021-12-14 22:12:12
【问题描述】:

我有多个输入字段,用户可以在其中输入数据,并且根据该输入,必须过滤列表。

在下面的代码中有用户数组,我尝试过滤 2 个字段数据,然后加入两个数组结果,然后删除重复数据,但这不能正常工作。

我的代码过滤器数组用于一个字段,但我无法过滤多个字段。

const users = [
    {
        "id": 1,
        "firstname": "John",
        "lastname": "Bravo",
        "email": "John@bravo.com",
        "specialty": "Surgeon",
        "location": "Los Angeles",
        "about": "Blablabla" 
    },
    {
        "id": 2,
        "firstname": "Phil",
        "lastname": "Kiwil",
        "email": "phil@kiwil.com",
        "specialty": "Business & Entrepreneurship",
        "location": "Location",
        "about": "Something"
    },
    {
        "id": 3,
        "firstname": "John",
        "lastname": "Doe",
        "email": "john@doe.com",        
        "specialty": "Surgeon",
        "location": "NY",
        "about": "Some text lorem ipsum"
    },
    {
        "id": 4,
        "firstname": "Jimi",
        "lastname": "Henix",
        "email": "jimi@henix.om",
        "specialty": "Expert",
        "location": "Woodstock",
        "about": "Something goes here",
    },
    {
        "id": 5,
        "firstname": "Janis",
        "lastname": "Jopn",
        "email": "janis@jopn.com",
        "specialty": "Surgeon",
        "location": "Los Angeles",
        "about": "Something something description text",
    }
]

我的代码

this.state = {
            users:[],
            filter:[],
            location:"",
            specialty:""
        }
const {location,specialty,users} = this.state;
        var trimedLocation = location.trim(); 
        var trimedSpecialty = specialty.trim(); 
        const filter = users.filter(user =>{
            if(location == "" || specialty == ""){
                return (user.location.toLowerCase().indexOf(trimedLocation.toLowerCase()) !== -1) &&
                (user.specialty.toLowerCase().indexOf(trimedSpecialty.toLowerCase()) !== -1)
            } else {
                return (user.location.toLowerCase().indexOf(trimedLocation.toLowerCase()) !== -1) ||
                (user.specialty.toLowerCase().indexOf(trimedSpecialty.toLowerCase()) !== -1)
            }
        });
        this.setState({filter});

【问题讨论】:

  • 我用工作代码编辑帖子。

标签: javascript reactjs d3.js


【解决方案1】:

不确定您是否需要同时过滤用户甚至位置。如果您打算过滤具有多种条件的用户,您可以尝试这种方法。

const userFiltered = users.filter(user =>{
 return user.location.toLowerCase().includes(trimedLocation.toLowerCase() &&
        user.specialty.toLowerCase().includes(trimedSpecialty.toLowerCase()) ;
});

这是和AND 条件。如果您想将&& 替换为||,可以将其更改为OR

更新: 这是一个处理部分字符串和nullundefined 值的工作示例。 https://codesandbox.io/s/great-rosalind-8ztzq?file=/src/App.tsx:1386-1387

【讨论】:

  • 我想过滤用户,例如:如果我添加用户的(外科医生)专业和用户的(洛杉矶)位置,则列表应显示所有拥有专业外科医生并在洛杉矶的用户。如果我只给出专业,结果应该显示所有具有该专业的用户。我已经使用此代码检查此练习应用程序link
  • 好的。所以使用上述代码的 AND 版本。
  • || 无法正常工作。当我进入位置(洛杉矶)并将专业留空时,它会显示所有数据,反之亦然。它应该只显示带有位置的数据(洛杉矶)。当我添加位置和专业时,无论是对还是错,它都会根据给定的输入向我显示正确的结果。
  • 您应该处理修剪后的变量中的null|undefined 值,并仅在它们有值时将它们添加到条件中。
  • 用一个简单的例子更新响应。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
  • 2017-01-23
  • 1970-01-01
  • 2018-06-23
  • 2021-11-28
相关资源
最近更新 更多