更新时间:2023-11-26 08:01:22
不是使用索引,类似于来自Rahul Tripathi的评论链接答案,我会使用修改后的版本拉取对象按名称而不是传递整个对象。
Rather than use index of, similar to the comment linked answer from Rahul Tripat I would use a modified version to pull the object by name rather than pass the entire object.
function pluckByName(inArr, name, exists)
{
for (i = 0; i < inArr.length; i++ )
{
if (inArr[i].name == name)
{
return (exists === true) ? true : inArr[i];
}
}
}
用法
// Find whether object exists in the array
var a = pluckByName(objectArray, 'A', true);
// Pluck the object from the array
var b = pluckByName(objectArray, 'B');