<分区>
我正在学习 NodeSchool 的 Functional Javascript 类(class)。 “Every Some”练习提供了一个“goodUsers”对象数组来比较另一个用户列表(也是一个对象数组)。
我希望有人可以帮助我想象解决方案中发生的事情:
function checkUsersValid(goodUsers) {
return function allUsersValid(submittedUsers) {
return submittedUsers.every(function(submittedUser) {
return goodUsers.some(function(goodUser) {
return goodUser.id === submittedUser.id;
});
});
};
}
module.exports = checkUsersValid;
这些是提供的说明:
# Task
Return a function that takes a list of valid users, and returns a function that returns true if all of the supplied users exist in the original list of users.
You only need to check that the ids match.
## Example
var goodUsers = [
{ id: 1 },
{ id: 2 },
{ id: 3 }
]
// `checkUsersValid` is the function you'll define
var testAllValid = checkUsersValid(goodUsers)
testAllValid([
{ id: 2 },
{ id: 1 }
])
// => true
testAllValid([
{ id: 2 },
{ id: 4 },
{ id: 1 }
])
// => false
## Arguments
* goodUsers: a list of valid users
Use array#some and Array#every to check every user passed to your returned function exists in the array passed to the exported function.