javascript - 如何根据客户的输入动态查找用户?[解决了]
问题描述
我正在尝试提供一种更动态的方式来根据用户的输入查找 Person 是否存在。我真的只是在寻找基于这个愚蠢例子的动态方式:
输入字段示例
let inputField = [
{
name: 'John Smith',
age: 25,
married: true
best_contact: 'email'
},
{
name: 'John Smith',
age: 24,
married: false,
best_contact: 'email'
},
{
name: 'John Smith',
age: 39,
married: false,
best_contact: 'phone'
}
]
用户输入示例
let user = {
name: 'John Smith',
age: 39,
married: false,
best_contact: 'email'
}
我的档案...
// this is what i have so far
inputField.map(input => {
return input.find(person => {
let userKeys = Object.keys(user);
let result = userKeys.reduce((a, v)) => {
if(v && person.hasOwnProperty(v)) {
if(v && input[v] === person[v]) a++;
else if(['phone', 'email', 'in-person'].includes(v) && person[v]) a++;
// age condition here i will leave out
}
return a;
}
return result === userKeys.length; // returns the inputField obj regardless matches users input value
}
}
// this should return false or nothing
在这种情况下,它应该不返回任何内容,因为结婚后的值不匹配。无论如何,我都会得到一场比赛,我相信我们这里的社区对此有一个更清洁、更动态的解决方案。我可以指定姓名、年龄、已婚、最好的联系方式,但我想要一个更简单的方法来做到这一点。
解决方案
推荐阅读
- excel - 使用 VBA 进行抓取
- ruby-on-rails - Rails“参数丢失或值为空”错误
- java - 如何使用 Java 在 a5 页面上打印 doc 文件?
- typescript - 在 Intellij/Webstorm 中的 html 中显示打字稿错误
- azure-devops - 我可以从 Azure Boards 创建 Github 功能分支吗?
- android - GridLayoutManager 如何在下一行显示不同的视图类型
- c - C 标准库中的任何函数是否隐式使用`stderr`?
- php - 从 PHP 文件调用“npm run generate”会导致“npm ERR!code ELIFECYCLE”
- javascript - Javascript React - 不导航到重定向页面
- android - 如何通过重新安装应用程序使房间数据库保持不变?