Javascript JS.filter()在传入intereger时起作用,但在传入变量时不起作用

Javascript JS.filter()在传入intereger时起作用,但在传入变量时不起作用,javascript,ecmascript-6,Javascript,Ecmascript 6,我正在尝试使用.filter从对象数组中提取对象 当我这样做时: var homeCountry = this.props.searchedCountry.filter(country => country.id === 7); var homeCountry = this.props.searchedCountry.filter(country => country.id === e.target.country.value); 我从过滤后的数组中得到一个值,但执行此操作时:

我正在尝试使用.filter从对象数组中提取对象

当我这样做时:

var homeCountry = this.props.searchedCountry.filter(country => country.id === 7);
var homeCountry = this.props.searchedCountry.filter(country => country.id === e.target.country.value);
我从过滤后的数组中得到一个值,但执行此操作时:

var homeCountry = this.props.searchedCountry.filter(country => country.id === 7);
var homeCountry = this.props.searchedCountry.filter(country => country.id === e.target.country.value);
其中e.target.country.value==7,我得到一个空数组


有人能解释发生了什么事吗?谢谢

e.target.value
是字符串值。您正在与数据类型和值进行严格的比较

更新代码

 var homeCountry = this.props.searchedCountry.filter(country => country.id === parseInt(e.target.country.value));

e.target.value
将是一个字符串,但有几种方法可以处理此问题,您可以使用
parseInt(e.target.value)
Number(e.target.value)
e.target.valueAsNumber
。其次,
==
检查值以及您正在比较的操作数的数据类型,因此(
1===“1”
)将被计算为false,但(
1===1
)将被计算为true,如果您想使用
=
,则执行
=
操作将只比较值(非严格意义上的等于)您不必使用上述建议的解决方案来处理问题,因此(
1==1
)是正确的(
1==“1”
)也是正确的,但是如果您希望使用
===
(这是可取的),请使用上述建议的解决方案

e.target.country.value
?这是一个输入错误还是你的
e.target
节点确实有一个
country
属性,它本身包含一个
value
属性?@DavidThomas不是一个输入错误:它可以工作,但现在我收到了通知“第12行:预期的”===”并看到了“==”eqeq“。有没有办法让它消失?我可以将e.target.country.value转换成int吗?
parseInt(e.target.country.value)
,或者干脆
(+e.target.country.value)
e.target.value
包装成
parseInt
函数,然后执行
==
@peter176更新我的答案