30

I have a json like so:

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?

shreyj
  • 1,759
  • 3
  • 22
  • 31

6 Answers6

114

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);
Gowsikan
  • 5,571
  • 8
  • 33
  • 43
  • What if i want to find KEY that contains watevr2.?? – Tushar Shukla Jul 02 '16 at 17:19
  • 1
    @TusharShukla It requires iteration to find key by its value. Please refer this [link](http://stackoverflow.com/questions/9907419/javascript-object-get-key-by-value) – Gowsikan Dec 07 '16 at 07:09
  • Is it stable or not? –  Jan 11 '18 at 22:40
  • I tried this solution on fiddle but it seems that I always got -1 as result. Here is the fiddle http://jsfiddle.net/k7c9uwz1/ .Am I missing something ? Please help – shifu Aug 31 '18 at 07:16
  • @shifu Coz the json is not object. it is considered as String coz it was with single quotes. I just removed the single quotes. Please check now. Updated fiddle link http://jsfiddle.net/k7c9uwz1/2/ – Gowsikan Aug 31 '18 at 09:22
13

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
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • 1
    Well, I was not looking for the loop to iterate over the json. Actually, in a piece of my code I create an array by iterating over the json. In another code piece I have a key from json and I have the array and I need to find the array element which relates to this key. I know I am in a strange position and you might be confused, but I got what I was looking for. so thanks! – shreyj Mar 06 '13 at 10:34
10

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

Jiri Tobisek
  • 579
  • 3
  • 5
5

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.

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
});
Mostafa Shahverdy
  • 2,687
  • 2
  • 30
  • 51
0

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

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

zz1433
  • 3,528
  • 2
  • 28
  • 36
-1

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);
Pradip Kharbuja
  • 3,442
  • 6
  • 29
  • 50