Javascript中匹配特定字符串的字符串方法是什么
What string method for matching specific strings in Javascript
这可能是一个简单的重复问题,但我在下面找不到任何解决方案来解决我的问题。请帮帮我! :)
我正在寻找一种字符串方法来检查值是否与特定字符串匹配以及 return 结果。
- 我正在使用
create-react-app
并且我有一个 <select>
下拉列表供用户过滤具有特定价格范围的菜肴
- 此下拉列表有 3 个字符串值:“$”、“$$”和“$” $$"
我目前正在使用 .includes
方法,但没有得到我想要的结果,因为它包括所有包含字符串中的字符的结果。 (如果用户选择“$”,也会return结果包括“$$”和“” $$$")
-> 我只希望他们 return 结果与上面的字符串值完全相同。
那么用什么代替 .includes
?
// Initial states:
const [foodList, setFoodList] = useState([])
const [foodName, setFoodName] = useState('')
const [isVegetarian, setIsVegetarian] = useState('')
const [priceRange, setPriceRange] = useState('$')
const [textSearch, setTextSearch] = useState('')
const [priceDropdown, setPriceDropdown] = useState('')
const [vegDropdown, setVegDropdown] = useState('')
// Search.js:
const newSearch = props.foodList.filter((value) => {
return (
value.foodName.toLowerCase().includes(textSearch.toLowerCase()) &&
// What to use instead of .includes here?
value.priceRange.toLowerCase().includes(priceDropdown.toLocaleLowerCase())
&& value.isVegetarian.includes(vegDropdown.toLowerCase())
)
}
)
看起来标准 ===
足以满足您的需求,因为您没有检查值 是否包含 过滤器选择,但是正是的选择。 (我怀疑您在 isVegetarian
选项上也需要相同的选项。
const newSearch = props.foodList.filter((value) => {
return (
value.foodName.toLowerCase().includes(textSearch.toLowerCase()) &&
value.priceRange.toLowerCase() === priceDropdown.toLocaleLowerCase() &&
value.isVegetarian === vegDropdown.toLowerCase()
)
});
此外,正如评论中提到的那样,尽量让您的示例尽可能少以重现问题 - 它始终是上下文和简单性之间的平衡。不过,在这种情况下,显示 foodList
的小型数据结构和 newSearch
方法(带有一些解释)可能就足够了。
这可能是一个简单的重复问题,但我在下面找不到任何解决方案来解决我的问题。请帮帮我! :)
我正在寻找一种字符串方法来检查值是否与特定字符串匹配以及 return 结果。
- 我正在使用
create-react-app
并且我有一个<select>
下拉列表供用户过滤具有特定价格范围的菜肴 - 此下拉列表有 3 个字符串值:“$”、“$$”和“$” $$"
我目前正在使用 .includes
方法,但没有得到我想要的结果,因为它包括所有包含字符串中的字符的结果。 (如果用户选择“$”,也会return结果包括“$$”和“” $$$")
-> 我只希望他们 return 结果与上面的字符串值完全相同。
那么用什么代替 .includes
?
// Initial states:
const [foodList, setFoodList] = useState([])
const [foodName, setFoodName] = useState('')
const [isVegetarian, setIsVegetarian] = useState('')
const [priceRange, setPriceRange] = useState('$')
const [textSearch, setTextSearch] = useState('')
const [priceDropdown, setPriceDropdown] = useState('')
const [vegDropdown, setVegDropdown] = useState('')
// Search.js:
const newSearch = props.foodList.filter((value) => {
return (
value.foodName.toLowerCase().includes(textSearch.toLowerCase()) &&
// What to use instead of .includes here?
value.priceRange.toLowerCase().includes(priceDropdown.toLocaleLowerCase())
&& value.isVegetarian.includes(vegDropdown.toLowerCase())
)
}
)
看起来标准 ===
足以满足您的需求,因为您没有检查值 是否包含 过滤器选择,但是正是的选择。 (我怀疑您在 isVegetarian
选项上也需要相同的选项。
const newSearch = props.foodList.filter((value) => {
return (
value.foodName.toLowerCase().includes(textSearch.toLowerCase()) &&
value.priceRange.toLowerCase() === priceDropdown.toLocaleLowerCase() &&
value.isVegetarian === vegDropdown.toLowerCase()
)
});
此外,正如评论中提到的那样,尽量让您的示例尽可能少以重现问题 - 它始终是上下文和简单性之间的平衡。不过,在这种情况下,显示 foodList
的小型数据结构和 newSearch
方法(带有一些解释)可能就足够了。