Javascript 从值中获取 JSON 密钥或反转 JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1970175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:10:37  来源:igfitidea点击:

Getting JSON Key from Value or Inverting JSON Data

javascriptjsonlookup

提问by Sampson

Getting single key from Value

从 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.

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

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

反转 JSON 数据

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'};

回答by philfreo

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:

但是,如果您需要多次执行此操作,您应该让代码重建 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);

回答by Luca Matteis

The only other logical solution would be to have the long namebe the keyand the abbreviationbe 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"}));