search json numbers

Possible Duplicate:
Validate numbers in JavaScript - IsNumeric()

I have the following (working) code that searches for the string "type 1" in my Json , but how can I search for a number? I am guessing that I need to change RegExp to Number, but I can't get it to work, it tells me that v.properties.code in not a function.

$.each(geojson.features, function (i, v) {
    if (v.properties.code.search(new RegExp(/type 1/i)) != -1) {
         Count++;
    }
});

Numbers doesn't have the search function in their prototypes, so you just need to convert it to a string, this way you ensure it will always be a string, and you won't get that error, even though you should be doing a proper check on your content

$.each(geojson.features, function (i, v) {
  if (v.properties.code.toString().search(/type 1/i) !== -1) {
     Count++;
  }
});

or the other (less typing) way

$.each(geojson.features, function (i, v) {
  if (/type 1/i.test(v.properties.code)) { // does the conversion automatically
     Count++;
  }
});
链接地址: http://www.djcxy.com/p/26410.html

上一篇: 如何验证在我的输入字段上正常工作

下一篇: 搜索json号码