gpt4 book ai didi

javascript - every() some() 和函数返回函数

转载 作者:行者123 更新时间:2023-11-29 16:06:48 26 4
gpt4 key购买 nike

<分区>

我正在学习 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.

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com