0

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 ?

1
  • That's an object, not an array. Commented Feb 12, 2015 at 17:12

1 Answer 1

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

var key = getObjectKeyFromValue(PrivateChatList, 'player4')
alert(key); // 'supporter4'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.