1

I'm having a bit of trouble getting the information I want out of a set of data (see below). Essentially, I need to get the fuel price for each different type of fuel. Originally I was working off the assumption that each item in the data had all 4 types of fuel, so if I wanted the diesel price, I could just say data.locations[i].fuelTypes[3].price. However, let's say a store doesn't have Premium gas - then diesel would be located at fuelTypes[2].

Is there a way to look up the item in the fuelTypes array based on its description? Right now the only thing I can think of would be to iterate over everything in the fuelTypes array and say

if (data.locations[i].fuelTypes[j].description == 'Diesel') {
    gasPrice = data.locations[i].fuelTypes[j].price;
}

But this seems like it would be inefficient over a large number of records.

Here's a simplified version of the data I'm working with.

var data = {
    "locations": [
        {
            "name": "Store 1",
            "fuelTypes": [
                {
                    "description": "Unleaded",
                    "price": "1.00",
                    "currency": "USD"
                },
                {
                    "description": "Plus",
                    "price": "2.00",
                    "currency": "USD"
                },
                {
                    "description": "Premium",
                    "price": "3.00",
                    "currency": "USD"
                },
                {
                    "description": "Diesel",
                    "price": "4.00",
                    "currency": "USD"
                }
                ]
        },
        {
            "name": "Store 2",
            "fuelTypes": [
                {
                    "description": "Unleaded",
                    "price": "1.00",
                    "currency": "USD"
                },
                {
                    "description": "Plus",
                    "price": "2.00",
                    "currency": "USD"
                },
                {
                    "description": "Premium",
                    "price": "3.00",
                    "currency": "USD"
                },
                {
                    "description": "Diesel",
                    "price": "4.00",
                    "currency": "USD"
                }
                ]
        }
        ]
};
Jack
  • 140
  • 12
  • can you change the JSON – meda Jul 15 '15 at 19:29
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Daniel Hoffmann-Mitscherling Jul 15 '15 at 19:29
  • Unfortunately I can't. It's being returned from an API, and that's the way it's set up. – Jack Jul 15 '15 at 19:30
  • If you wish to know how to access a value, see the duplicate post. If you wish to answer "But this seems like it would be inefficient over a large number of records.", how do you think any function or library you use will determine what the property of your object is? At some level, you must iterate over your object. – Daniel Hoffmann-Mitscherling Jul 15 '15 at 19:31
  • 3
    Short answer: No. You're going to have to iterate over all of the records. – Brett Jul 15 '15 at 19:32

2 Answers2

0

Another approach, since it hasn't been mentioned is to use JSON path.

$..fuelTypes[?(@.description=='Diesel')]

Would return:

[{
    "description": "Diesel",
    "price": "4.00",
    "currency": "USD"
},
{
    "description": "Diesel",
    "price": "4.00",
    "currency": "USD"
}]
Blazes
  • 4,721
  • 2
  • 22
  • 29
0

If I understand correctly, the essential problem is that you need to convert your data from being indexed to being key=>value.

If you had a function like this:

function toKeyValue(fuelTypesArray) {
    ob = {};
    for (var i = 0; i < fuelTypesArray.length; i++) {
        ob[fuelTypesArray[i].description] = {
            price: fuelTypesArray[i].price,
            currency: fuelTypesArray[i].currency
        };
    }
    return ob;
}

Then instead of accessing Diesel unreliably like so:

data.locations[i].fuelTypes[3].price

You could use

toKeyValue(data.locations[i].fuelTypes)["Diesel"].price

Travis
  • 1,998
  • 1
  • 21
  • 36