私の全体のコード例は、以下と jsFiddle にあります: http://jsfiddle.net/rEhvt/。
alert()
多次元の JavaScript オブジェクトを作成しようとしています。オブジェクトalert()
の単一のプロパティである場合、機能します。例えば:
alert(votes['77777'].stay);
alert()
ただし、オブジェクト全体を取得しようとすると、多くのNULL
s または空白が表示されます。私は次のことを試しました:
alert(JSON.stringify(votes));//returns lots of NULLs
alert(votes.join('\n'));//returns lots of blanks
私の意図は、最終的にオブジェクトをlocalStorageに送信することです
localStorage['votes']=JSON.stringify(votes);
そしてそれを取得します
retrievedVotes=JSON.parse(localStorage['votes'])
ただし、最初にオブジェクト構造を確認できるようにしたいと考えています。これは、JavaScript を使用して多次元オブジェクトを理解する最初の試みです。以下は、jsFiddle にもある私のコードのすべてです: http://jsfiddle.net/rEhvt/。
//Movie #1
var movieID = 55555;
var stay = 'yes';
var votes = [];
votes[movieID]=[];
votes[movieID].stay = stay;
var scenes = 1;
votes[movieID].scenes = scenes;
//Movie #2
var movieID = 77777;
var stay = 'no';
var votes = [];
votes[movieID]=[];
votes[movieID].stay = stay;
var scenes = 0;
votes[movieID].scenes = scenes;
//Alert Single Item from votes
alert(votes['77777'].stay);
//Alert entire object
alert(JSON.stringify(votes));//returns NULLs
//Alert entire object
alert(votes.join('\n'));//returns lots of blanks