简体   繁体   English

如何基于值访问JSON对象中的特定项目?

[英]How can I access a specific item in JSON object based on a value?

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 . 最初,我是在假设数据中的每一项都具有所有4种燃料,因此,如果我想要柴油价格,我可以说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] . 但是,假设一家商店没有高级汽油-则柴油将位于fuelTypes[2]

Is there a way to look up the item in the fuelTypes array based on its description? 有没有一种方法可以根据其描述在fuelTypes数组中查找该项目? Right now the only thing I can think of would be to iterate over everything in the fuelTypes array and say 现在,我唯一能想到的就是遍历fuelTypes数组中的所有内容并说

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"
                }
                ]
        }
        ]
};

Another approach, since it hasn't been mentioned is to use JSON path. 由于尚未提及,因此另一种方法是使用JSON路径。

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

Would return: 将返回:

[{
    "description": "Diesel",
    "price": "4.00",
    "currency": "USD"
},
{
    "description": "Diesel",
    "price": "4.00",
    "currency": "USD"
}]

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: 然后,不要像这样不可靠地访问Diesel:

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

You could use 你可以用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM