【发布时间】:2020-12-04 14:37:39
【问题描述】:
TLDR; 您将如何制作一个功能,例如搜索价值,例如在数组中,但是搜索的属性在哪里可以改变?
示例。
const search = (arr, text) =>
{
// What if rather than e.name we wanted to search on e.phone?
// And how could this be passed with the function params?
return arr.find(e => e.name === text)
}
【问题讨论】:
-
您可以使用括号表示法来访问任何属性。见这里:Dynamically access object property using variable
-
哦,哇,我什至不知道那是一回事。非常感谢人:)
-
添加另一个参数
(arr, prop, text)并使用e[prop] === text
标签: javascript arrays