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 ?
-
That's an object, not an array.Andy– Andy2015年02月12日 17:12:33 +00:00Commented Feb 12, 2015 at 17:12
1 Answer 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
lang-js