**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。
这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
1小时前关闭。
Improve this question
我想只过滤颜色为红色的人作为favColor...我该怎么做?这是我尝试的:但不起作用
const filteredpersonen = personen.filter(personen => personen.favColor= 'red');
console.log(filteredpersonen);
这是我的代码:
let personen = [
{
naam : 'jan',
age : 41,
favColor:[
'blue',
'green',
'yellow',
'orange'
]
},
{
naam : 'james',
age : 31,
favColor:[
'red',
'black',
'yellow',
'purple'
]
},
{
naam : 'linda',
age : 21,
favColor:[
'blue',
'white',
'red',
'grey'
]
},
{
naam : 'marya',
age : 31,
favColor:[
'creme',
'green',
'orange',
'red'
]
}
]
3条答案
按热度按时间piok6c0g1#
=
是赋值,==
或===
是比较favColor
是一个数组,所以执行favColor === 'red'
没有意义使用
.includes
jvidinwx2#
只需要使用includes方法来查找数组中的颜色。
zqry0prt3#
您当前正在 * 分配 * 值
'red'
给每个人的favColor
,而不是进行任何类型的比较。看起来您试图使用==
(比较)或===
(严格比较);这将尝试将字符串('red'
)与数组进行比较(检查数组是否 * 等于 * 字符串),数组始终为false
。相反,您需要检查值
'red'
是否在数组中。数组有一个方法,名为includes()
。