我可以在if()条件中比较数组的特定值吗?
例:
// teacher object
function Prof(id,tab,nome,ocupacao,email,tel,foto)
{
this.id = id;
this.tab = tab;
this.nome = nome;
this.ocupacao= ocupacao;
this.email = email;
this.tel = tel;
this.foto = foto;
}
//teachers array
var arr = new Array();
arr[0] = new Prof('pf1','dir','Mario','Diretor','mail@mail.com','tel','foto');
arr[1] = new Prof('pf2','dir','Joao','t1 t3 t7','...','...','...');
...
// So now i will get the array value of "ocupacao" and see if this value has
// an specific value that i´ve defined in the conditional:
...
if(arr[i].ocupacao (has value) t7)
{
//do something
}
请注意;我可以在PHP或ASP中使用一些东西:“LIKE”或“%value%”?
答案 0 :(得分:6)
if(arr[i].ocupacao.indexOf('t7') >= 0)
{
// do something
}
答案 1 :(得分:1)
我猜你指的是 ocupacao数组,但你本身就意味着ocupacao字符串是由许多以空格分隔的单词组成的。
然后你可以用三种方式做到这一点:
$.inArray(...)
,否则你将需要遍历数组并搜索它Array.indexOf
如果找不到项目将返回-1