从值获取 JSON 键或反转 JSON 数据

发布于 2024-08-16 01:43:18 字数 1316 浏览 4 评论 0原文

从 Value 获取单个键

我想从以下 JSON 中进行向后选择。我想提取特定州的缩写。在这种情况下,缩写是关键,而我开始的值就是值。

当然,我可以循环遍历每个值,将该值与我的值进行比较,并在匹配时选择键。这是解决此类问题的最佳方法吗?或者有更好的方法吗?

反转 JSON 数据

另一种选择是在处理早期反转此数据,以便为自己提供一组类似的值,并交换键/值。我也有兴趣了解有效执行此操作的方法。

var States = {AL: 'Alabama', AK: 'Alaska', AZ: 'Arizona', AR: 'Arkansas', 
              CA: 'California', CO: 'Colorado', CT: 'Connecticut', 
              DE: 'Delaware', DC: 'District of Columbia', FL: 'Florida', 
              GA: 'Georgia', HI: 'Hawaii', ID: 'Idaho', IL: 'Illinois', 
              IN: 'Indiana', IA: 'Iowa', KS: 'Kansas', KY: 'Kentucky', 
              LA: 'Louisiana', ME: 'Maine', MD: 'Maryland', MA: 'Massachusetts', 
              MI: 'Michigan', MN: 'Minnesota', MO: 'Missouri', MT: 'Montana', 
              NE: 'Nebraska', NV: 'Nevada', NH: 'New Hampshire', 
              NJ: 'New Jersey', NM: 'New Mexico', NY: 'New York', 
              NC: 'North Carolina', ND: 'North Dakota', OH: 'Ohio', 
              OK: 'Oklahoma', OR: 'Oregon', PA: 'Pennsylvania', 
              RI: 'Rhode Island', SC: 'South Carolina', 
              SD: 'South Dakota', TN: 'Tennessee', TX: 'Texas', UT: 'Utah', 
              VT: 'Vermont', VA: 'Virginia', WA: 'Washington', 
              WV: 'West Virginia', WI: 'Wisconsin', WY: 'Wyoming'};

Getting single key from Value

I would like to do a backwards selection from the following JSON. I'd like to extract the abbreviation for a particular state. In this situation, the abbreviation is the key, and the value that I'm starting with is the value.

Certainly I can loop through each value, comparing the value to my value, and select the key when the match is made. Is this the best way to approach something like this? Or is there a better way?

Inverting JSON Data

Another option would to invert this data early in processing to give myself a similar set of values with the keys/values swapped. I would be interested in seeing methods for doing this efficiently as well.

var States = {AL: 'Alabama', AK: 'Alaska', AZ: 'Arizona', AR: 'Arkansas', 
              CA: 'California', CO: 'Colorado', CT: 'Connecticut', 
              DE: 'Delaware', DC: 'District of Columbia', FL: 'Florida', 
              GA: 'Georgia', HI: 'Hawaii', ID: 'Idaho', IL: 'Illinois', 
              IN: 'Indiana', IA: 'Iowa', KS: 'Kansas', KY: 'Kentucky', 
              LA: 'Louisiana', ME: 'Maine', MD: 'Maryland', MA: 'Massachusetts', 
              MI: 'Michigan', MN: 'Minnesota', MO: 'Missouri', MT: 'Montana', 
              NE: 'Nebraska', NV: 'Nevada', NH: 'New Hampshire', 
              NJ: 'New Jersey', NM: 'New Mexico', NY: 'New York', 
              NC: 'North Carolina', ND: 'North Dakota', OH: 'Ohio', 
              OK: 'Oklahoma', OR: 'Oregon', PA: 'Pennsylvania', 
              RI: 'Rhode Island', SC: 'South Carolina', 
              SD: 'South Dakota', TN: 'Tennessee', TX: 'Texas', UT: 'Utah', 
              VT: 'Vermont', VA: 'Virginia', WA: 'Washington', 
              WV: 'West Virginia', WI: 'Wisconsin', WY: 'Wyoming'};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三寸金莲 2024-08-23 01:43:18

没有“自动”方法可以做到这一点。您唯一的选择是循环遍历列表,直到找到与键匹配的值。

但是,如果您需要多次执行此操作,则应该让代码重建 JSON 对象并交换键/值,以便将来的查找速度更快。一个简单的方法:

function swapJsonKeyValues(input) {
    var one, output = {};
    for (one in input) {
        if (input.hasOwnProperty(one)) {
            output[input[one]] = one;
        }
    }
    return output;
}

var stateAbbrs = swapJsonKeyValues(States);

There's no "automatic" way to do this. Your only option is to loop through the list until you find the value that matches the key.

But, if you need to do this multiple times, you should have the code rebuild the JSON object with key/values swapped, so that future lookups are faster. A simple way:

function swapJsonKeyValues(input) {
    var one, output = {};
    for (one in input) {
        if (input.hasOwnProperty(one)) {
            output[input[one]] = one;
        }
    }
    return output;
}

var stateAbbrs = swapJsonKeyValues(States);
弥枳 2024-08-23 01:43:18

唯一的其他合乎逻辑的解决方案是将长名称作为缩写作为。通常是在键上进行选择,因为它们应该是唯一的。

通过第一次执行键-值切换,您可以避免每次想要获取值时都进行迭代。

function switcharoo(o) {
    var t = {};
    for (var i in o) {
        if(o.hasOwnProperty(i)){
            t[o[i]] = i ;
        }
    }
    return t;
}

console.log(switcharoo({AZ: "Arizona"}));

The only other logical solution would be to have the long name be the key and the abbreviation be the value. Selection is usually made on keys since they are unique, as they should.

You could save yourself from iterating every time you want to get the value, by doing the key - value switch the first time.

function switcharoo(o) {
    var t = {};
    for (var i in o) {
        if(o.hasOwnProperty(i)){
            t[o[i]] = i ;
        }
    }
    return t;
}

console.log(switcharoo({AZ: "Arizona"}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文