Possible Duplicate:
Validate numbers in JavaScript - IsNumeric()
我有以下(工作)代码,用于在我的 Json
中搜索字符串“type 1” ,但是如何搜索号码呢?我猜我需要改变RegExp
到 Number,但我无法让它工作,它告诉我 v.properties.code
不是一个函数。
$.each(geojson.features, function (i, v) {
if (v.properties.code.search(new RegExp(/type 1/i)) != -1) {
Count++;
}
});
最佳答案
Numbers 的原型(prototype)中没有搜索功能,因此您只需将其转换为字符串,这样您就可以确保它始终是字符串,并且您不会收到该错误,即使您应该这样做对您的内容进行适当的检查
$.each(geojson.features, function (i, v) {
if (v.properties.code.toString().search(/type 1/i) !== -1) {
Count++;
}
});
或其他(更少打字)方式
$.each(geojson.features, function (i, v) {
if (/type 1/i.test(v.properties.code)) { // does the conversion automatically
Count++;
}
});
关于javascript - 搜索 json 数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14066716/