在 Javascript 中过滤(搜索)两个值(搜索条件)
Filtering (searching) for two values (search criteria) in Javascript
我正在尝试设置一个采用两个搜索条件(名称 and/or 标签)的应用程序。
我为按名称搜索设置了动态搜索。但我无法进一步过滤标签名称的搜索。如何设置动态搜索 2 个值。在我的情况下,您应该能够通过 name && tag
或仅 name
或仅 tag
.
进行搜索
const filteredResults = studentData.filter((student) => {
const name = student.firstName + " " + student.lastName;
return (
name.toLowerCase().includes(searchTerm.toLowerCase()) ||
student.tags.filter((tag) => {
return tag.toLowerCase().includes(tagTerm.toLowerCase());
})
);
});
这是 api 数据的一个条目:
{
city: "Fushë-Muhurr"
company: "Yadel"
email: "iorton0@imdb.com"
firstName: "Ingaberg"
grades: [ "78", "100", "92", … ]
id: "1"
lastName: "Orton"
pic: "https://storage.googleapis.com/hatchways-app.appspot.com/assessments/data/frontend/images/voluptasdictablanditiis.jpg"
skill: "Oracle"
tags: [ "Tag1", "Tag2" ]
}
如果有人想看的话,我在这里链接我的 gitbo 存储库(这是一个非常小的项目):
https://github.com/EricPezzulo/HatchwaysFrontendAssessment
你可以试试这个
const result = studentData.filter(({
firstName,
lastName,
tags
}) => new RegExp(searchTerm, 'gui').test(`${firstName} ${lastName} ${tags.join(' ')}`));
如评论中所述,您正在尝试过滤两者或仅过滤其中之一。
以下代码应该会恢复您一直要求的内容
const studentsArray = [{
city: "Fushë-Muhurr",
company: "Yadel",
email: "iorton0@imdb.com",
firstName: "Ingaberg",
grades: [ "78", "100", "92"],
id: "1",
lastName: "Orton",
pic: "https://storage.googleapis.com/hatchways-app.appspot.com/assessments/data/frontend/images/voluptasdictablanditiis.jpg",
skill: "Oracle",
tags: [ "Tag1", "Tag2" ],
},
{
city: "Fushë-Muhurr",
company: "Yadel",
email: "iorton0@imdb.com",
firstName: "Ingaberg",
grades: [ "78", "100", "92"],
id: "1",
lastName: "Orton",
pic: "https://storage.googleapis.com/hatchways-app.appspot.com/assessments/data/frontend/images/voluptasdictablanditiis.jpg",
skill: "Oracle",
tags: [ "Tag3", "Tag4" ],
}]
const searchTerm = ''
const tagTerm = ''
const filteredResults = studentsArray.filter((student) => {
const name = student.firstName + ' ' +student.lastName;
const nameIncludesSearchTerm = name.toLowerCase().includes(searchTerm.toLowerCase())
const tagsIncludesTagTerm = student.tags.map(t=>t.toLowerCase()).includes(tagTerm.toLowerCase())
const includesNameAndTag = nameIncludesSearchTerm && tagsIncludesTagTerm
const includeNameOrTag = nameIncludesSearchTerm || tagsIncludesTagTerm
return includesNameAndTag || includeNameOrTag;
});
console.log({filteredResults})
我正在尝试设置一个采用两个搜索条件(名称 and/or 标签)的应用程序。
我为按名称搜索设置了动态搜索。但我无法进一步过滤标签名称的搜索。如何设置动态搜索 2 个值。在我的情况下,您应该能够通过 name && tag
或仅 name
或仅 tag
.
const filteredResults = studentData.filter((student) => {
const name = student.firstName + " " + student.lastName;
return (
name.toLowerCase().includes(searchTerm.toLowerCase()) ||
student.tags.filter((tag) => {
return tag.toLowerCase().includes(tagTerm.toLowerCase());
})
);
});
这是 api 数据的一个条目:
{
city: "Fushë-Muhurr"
company: "Yadel"
email: "iorton0@imdb.com"
firstName: "Ingaberg"
grades: [ "78", "100", "92", … ]
id: "1"
lastName: "Orton"
pic: "https://storage.googleapis.com/hatchways-app.appspot.com/assessments/data/frontend/images/voluptasdictablanditiis.jpg"
skill: "Oracle"
tags: [ "Tag1", "Tag2" ]
}
如果有人想看的话,我在这里链接我的 gitbo 存储库(这是一个非常小的项目): https://github.com/EricPezzulo/HatchwaysFrontendAssessment
你可以试试这个
const result = studentData.filter(({
firstName,
lastName,
tags
}) => new RegExp(searchTerm, 'gui').test(`${firstName} ${lastName} ${tags.join(' ')}`));
如评论中所述,您正在尝试过滤两者或仅过滤其中之一。
以下代码应该会恢复您一直要求的内容
const studentsArray = [{
city: "Fushë-Muhurr",
company: "Yadel",
email: "iorton0@imdb.com",
firstName: "Ingaberg",
grades: [ "78", "100", "92"],
id: "1",
lastName: "Orton",
pic: "https://storage.googleapis.com/hatchways-app.appspot.com/assessments/data/frontend/images/voluptasdictablanditiis.jpg",
skill: "Oracle",
tags: [ "Tag1", "Tag2" ],
},
{
city: "Fushë-Muhurr",
company: "Yadel",
email: "iorton0@imdb.com",
firstName: "Ingaberg",
grades: [ "78", "100", "92"],
id: "1",
lastName: "Orton",
pic: "https://storage.googleapis.com/hatchways-app.appspot.com/assessments/data/frontend/images/voluptasdictablanditiis.jpg",
skill: "Oracle",
tags: [ "Tag3", "Tag4" ],
}]
const searchTerm = ''
const tagTerm = ''
const filteredResults = studentsArray.filter((student) => {
const name = student.firstName + ' ' +student.lastName;
const nameIncludesSearchTerm = name.toLowerCase().includes(searchTerm.toLowerCase())
const tagsIncludesTagTerm = student.tags.map(t=>t.toLowerCase()).includes(tagTerm.toLowerCase())
const includesNameAndTag = nameIncludesSearchTerm && tagsIncludesTagTerm
const includeNameOrTag = nameIncludesSearchTerm || tagsIncludesTagTerm
return includesNameAndTag || includeNameOrTag;
});
console.log({filteredResults})