我想搜索这个由对象组成的员工数组,我应该能够搜索任何文本,例如如果我通过 ---> search_for_string_in_array ('aaron', employees) ;它应该向我显示“数组中存在值”,或者如果它的方式请帮助我..
//here is the employeee array ...
var employees =[{
name:"jacob",
age :23,
city:"virginia",
yoe :12,
image :'a.jpg'
},
{
name:"aaron",
age :21,
city:"virginia",
yoe :12,
image :'b.jpg'
},
{
name:"johnny",
age :50,
city:"texas",
yoe :12,
image :'c.jpg'
},
{
name:"jacob",
age :12,
city:"virginia",
yoe :12,
image :'a.jpg'
}];
这是在数组内执行搜索功能的函数。
function search_for_string_in_array(search_for_string, employees)
{
for (var i=0; i < employees.length; i++)
{
if (employees[i].match(search_for_string))
{
return 'Value exists in array';
}
}
return 'Value does NOT exist in array';
}
只需将要搜索的值和数组传递给函数,它就会告诉您字符串是否作为数组值的一部分存在。