【发布时间】:2011-08-10 02:34:46
【问题描述】:
我需要用一个搜索词的对象搜索一个对象数组,并在一个数组中获取结果的索引。
假设我有一个这样的数组:
[
{
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"
}
什么是搜索数组的最简洁和最高效的方法?
谢谢。
【问题讨论】:
-
你应该从你提供的数组中得到 [0, 3]。
标签: javascript search node.js underscore.js