有没有大佬知道为什么return item.age % 2 == 0会返回一个数组,而不是ture和false呢?欢迎留言!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var students = [{
studentName: "小明",
age: 16,
school: "清华",
}, {
studentName: "小黑",
age: 17,
school: "北大"
}, {
studentName: "小红",
age: 18,
school: "浙大"
}, {
studentName: "小红",
age: 15,
school: "浙大"
}]
aa()
console.log('========================')
bb()
function aa() {
let results = students.filter((item, i) => {
return c = item.age % 2 == 0
// 只是把item.age % 2 == 0的值给了c所以输出的为false,而函数真正return的是item.age % 2 == 0,return返回的结果为一个数组
})
console.log(c) //false
console.log(results) //数组
}
function bb() {
let = results = students.filter((item, i) => {
return item.age % 2 == 0
})
console.log(results) //数组
}
</script>
</body>
</html>