web-dev-qa-db-ja.com

変数を使用してオブジェクトプロパティにアクセスする方法

私はオブジェクトを持っています

var Messages = {
'fullName' : 'Tell us your cool name dude..! e.g. Yousuf Iqbal',
'userName' : 'Choose a catchy username. Remember! It should be available :)',
'password' : 'Choose a top secret password with special chars, numbers and alphabets',
'rePassword' : 'Retype the password you just typed. But, don\'t try to copy!',
'bYear' : 'Tell the year, in which this bomb blasted'
};

および変数。

var attribute = $('#userinfo form input').attr('name');

このようにこの変数を使用してMessagesオブジェクトプロパティを選択したいと思います。

var message = Messages.attribute;

しかし、それは機能していません..そしてまた以下を試しました..

var message = Messages+'.'+attribute;
20
Yousuf Memon

角括弧:

message = Messages[ attribute ];
42
Pointy
var message = Messages[attribute];
5
Lior Cohen