我需要搜索具有搜索項的對象的對象數組,並獲取數組中的結果索引。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