我有一个像这样的结构的json文件:
{ "gasStationID":"441", "gasStationLat":"39.6337420", "gasStationLong":"22.4324412", "fuelCompID":"6", "fuelCompNormalName":"AVIN", "ddID":"42010100", "municipalityID":"42010000", "countyID":"42000000", "phone1":null, "username":"user1", "fuelTypeID":"1", "fuelPrice":"1.379" }, { "gasStationID":"441", "gasStationLat":"39.6337420", "gasStationLong":"22.4324412", "fuelCompID":"6", "fuelCompNormalName":"AVIN", "ddID":"42010100", "municipalityID":"42010000", "countyID":"42000000", "phone1":null, "username":"user1", "fuelTypeID":"1", "fuelPrice":"1.478" }所以,我的问题是如何检索fuelTypeID=1的fuelPrice ?
我的JavaScript文件是这样的:
for (i = 0; i < obj.length; i++) { var _gasStationCompName = obj[i].fuelCompNormalName; var priceID = obj[i].fuelTypeID; switch (fuelId) { case '1': case priceID == 1: price = obj[i].fuelPrice; break; case '2': case priceID == 2: price = obj[i].fuelPrice; break; default: price = "0"; }我的问题是,在price = obj[i].fuelPrice仅显示fuelType=2的fuelPrice 。 我怎么能在switch语句里面过滤结果?
在此之前,我正在解析JSON文件。 此外,你可以看到我有case: '1' 。 这是因为我在我的HTML文件中有这样的内容:
<div> <select onChange="getData(this.value);"> <option value="1">Gas</option> <option value="2">Diesel</option> </select> </div>I have a json file with a structure like this:
{ "gasStationID":"441", "gasStationLat":"39.6337420", "gasStationLong":"22.4324412", "fuelCompID":"6", "fuelCompNormalName":"AVIN", "ddID":"42010100", "municipalityID":"42010000", "countyID":"42000000", "phone1":null, "username":"user1", "fuelTypeID":"1", "fuelPrice":"1.379" }, { "gasStationID":"441", "gasStationLat":"39.6337420", "gasStationLong":"22.4324412", "fuelCompID":"6", "fuelCompNormalName":"AVIN", "ddID":"42010100", "municipalityID":"42010000", "countyID":"42000000", "phone1":null, "username":"user1", "fuelTypeID":"1", "fuelPrice":"1.478" }So, my question is how to retrieve the fuelPrice with fuelTypeID=1?
My JavaScript file is this:
for (i = 0; i < obj.length; i++) { var _gasStationCompName = obj[i].fuelCompNormalName; var priceID = obj[i].fuelTypeID; switch (fuelId) { case '1': case priceID == 1: price = obj[i].fuelPrice; break; case '2': case priceID == 2: price = obj[i].fuelPrice; break; default: price = "0"; }My problem is that in the price = obj[i].fuelPrice is showing only the fuelPrice with fuelType=2. How can I filter inside the switch statement the result?
Before that I'm parsing the JSON file. Also, you can see that I have case: '1'. That's because I have something like this in my HTML file:
<div> <select onChange="getData(this.value);"> <option value="1">Gas</option> <option value="2">Diesel</option> </select> </div>最满意答案
变量和对象键区分大小写(A!= a);
for (i = 0; i < obj.length; i++) { var _gasStationCompName = obj[i].fuelCompNormalName; var priceID = obj[i].fuelTypeID; // fuelTypeId => fuelTypeID switch (priceID) { fuelTypeId // => priceID case '1': case priceID == 1: price = obj[i].fuelPrice; break; case '2': case priceID == 2: price = obj[i].fuelPrice; break; default: price = "0"; } }I managed to find the solution. Here is the right switch statement that works like a charm:
var priceID = obj[i].fuelTypeID; switch (fuelTypeID) { case '1': if (priceID == 1) { price = obj[i].fuelPrice; } break; case '2': if (priceID == 2) { price = obj[i].fuelPrice; } break; default: price = 0; }更多推荐
发布评论