获取json中键的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15218448/
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-09-03 18:46:47  来源:igfitidea点击:

Get index of a key in json

javascriptjson

提问by shreyj

I have a json like so:

我有一个像这样的json:

json = { "key1" : "watevr1", "key2" : "watevr2", "key3" : "watevr3" }

Now, I want to know the index of a key, say "key2" in json - which is 1. Is there a way?

现在,我想知道一个键的索引,在 json 中说“key2”——它是 1。有办法吗?

回答by Gowsikan

Its too late, but it may be simple and useful

为时已晚,但它可能很简单有用

var json = { "key1" : "watevr1", "key2" : "watevr2", "key3" : "watevr3" };
var keytoFind = "key2";
var index = Object.keys(json).indexOf(keytoFind);
alert(index);

回答by h2ooooooo

You don't need a numerical index for an object key, but many others have told you that.

您不需要对象键的数字索引,但许多其他人已经告诉过您。

Here's the actual answer:

这是实际的答案:

var json = { "key1" : "watevr1", "key2" : "watevr2", "key3" : "watevr3" };

console.log( getObjectKeyIndex(json, 'key2') ); 
// Returns int(1) (or null if the key doesn't exist)

function getObjectKeyIndex(obj, keyToFind) {
    var i = 0, key;

    for (key in obj) {
        if (key == keyToFind) {
            return i;
        }

        i++;
    }

    return null;
}

Though you're PROBABLY just searching for the same loop that I've used in this function, so you can go through the object:

尽管您可能只是在搜索我在此函数中使用的相同循环,但您可以遍历对象:

for (var key in json) {
    console.log(key + ' is ' + json[key]);
}

Which will output

哪个会输出

key1 is watevr1
key2 is watevr2
key3 is watevr3

回答by Jiri Tobisek

In principle, it is wrongto look for an index of a key. Keys of a hash map are unordered, you should never expect specific order.

原则上,查找键的索引是错误的。哈希映射的键是无序的,你永远不应该期望特定的顺序。

回答by Mostafa Shahverdy

What you have is a string representing a JSON serialized javascript object. You need to deserialize it back a javascript object before being able to loop through its properties. Otherwise you will be looping through each individual character of this string.

您拥有的是一个表示 JSON 序列化 javascript 对象的字符串。在能够循环遍历其属性之前,您需要将其反序列化回一个 javascript 对象。否则,您将遍历此字符串的每个单独字符。

var resultJSON = '{ "key1" : "watevr1", "key2" : "watevr2", "key3" : "watevr3" }';
    var result = $.parseJSON(resultJSON);
    $.each(result, function(k, v) {
        //display the key and value pair
        alert(k + ' is ' + v);
    });

or simply:

或者干脆:

arr.forEach(function (val, index, theArray) {
    //do stuff
});

回答by zz1433

What you are after are numerical indexes in the way classic arrays work, however there is no such thing with json object/associative arrays.

您所追求的是经典数组工作方式中的数字索引,但是 json 对象/关联数组没有这样的东西。

"key1", "key2" themeselves are the indexes and there is no numerical index associated with them. If you want to have such functionality you have to assiciate them yourself.

“key1”、“key2”本身就是索引,没有与它们关联的数字索引。如果你想拥有这样的功能,你必须自己关联它们。

回答by Pradip Kharbuja

Try this

尝试这个

var json = '{ "key1" : "watevr1", "key2" : "watevr2", "key3" : "watevr3" }';
json = $.parseJSON(json);

var i = 0, req_index = "";
$.each(json, function(index, value){
    if(index == 'key2'){
        req_index = i;
    }
    i++;
});
alert(req_index);