ciaksoy
ciaksoy

Reputation: 91

Find array value

My array:

var PrivateChatList = [];

And push key and value (just example):

PrivateChatList['supporter1'] = 'player1';
PrivateChatList['supporter2'] = 'player2';
PrivateChatList['supporter3'] = 'player3';
PrivateChatList['supporter4'] = 'player4';
PrivateChatList['supporter5'] = 'player5';
PrivateChatList['supporter6'] = 'player6';
PrivateChatList['supporter7'] = 'player7';

I want to find "player4" key on function. How can i find ?

Upvotes: 0

Views: 38

Answers (1)

Mex
Mex

Reputation: 999

function getObjectKeyFromValue(object, value)
{
    for(var k in object)
    {
        if(object[k] == value)
        {
            return k;
        }
    }
    return '';
}

var key = getObjectKeyFromValue(PrivateChatList, 'player4')
alert(key); // 'supporter4'

Upvotes: 1

Related Questions