search for element within an array

前端 未结 3 1379

My data[l][m] contains 1,2,3,4,5 I\'m trying to search for a particular number, say \'2\' in it. Is there a better way to do this?

for          


        
3条回答
  •  囚心锁ツ
    2021-01-29 01:13

    If you're already including jQuery, use $.inArray(), like this:

    if($.inArray(num, data[l][m]) > -1) {
      number = data[l][0];
      document.form.options[l-1] = new Option(number,number,true,true);
    }
    

    The shorter vanilla JS version is a direct .indexOf() on the Array, but IE doesn't have this by default.

提交回复
热议问题