我需要搜索具有搜索项的对象的对象数组,并获取数组中的结果索引。javascript根据搜索条件查找对象
比方说,我有一个这样的数组:
[
{
name: "Mary",
gender: "female",
country: "USA",
orientation: "straight",
colorChoice: "red",
shoeSize: 7
},
{
name: "Henry",
gender: "male",
country: "USA",
orientation: "straight",
colorChoice: "red",
},
{
name: "Bob",
colorChoice: "yellow",
shoeSize: 10
},
{
name: "Jenny",
gender: "female",
orientation: "gay",
colorChoice: "red",
}
]
现在我需要搜索数组:
{
gender: "female"
}
,并得到结果:
[ 0, 3 ]
搜索对象可以是任意长度:
{
gender: "female",
colorChoice: "red"
}
什么是最清洁和最高性能的方式来搜索数组?
谢谢。
相关:http://stackoverflow.com/questions/3624741/searching-for-objects-in-javascript-arrays – 2011-04-21 08:01:57
你应该得到[0,3]从数组你当下。 – KooiInc 2011-04-21 08:20:16
@KooiInc谢谢 – Harry 2011-04-21 08:33:56