我的数组:

var PrivateChatList = [];


并按下键和值(仅作为示例):

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


我想在功能上找到“ player4”键。我如何找到?

最佳答案

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

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

关于javascript - 查找数组值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28483562/

10-09 15:36