0

I have a multidimensional array named "notes". It has the fields "note1", "note2", and "note3".

I have a variable named "noteNum" that can be set to 1, 2, or 3.

If "noteNum = 2", then "notes[0].note + noteNum" should be equal to "notes[0].note2". BUT I can't seem to attach the variable? Why not?

My code is below:

// Array constructor
function notesConstructor(note1, note2, note3) {
    this.note1 = note1;
    this.note2 = note2;
    this.note3 = note3;
}

var notes = new Array();

notes[0] = new notesConstructor("Note 1A example note", "Note 2A example note", "Note 3A example note");
notes[1] = new notesConstructor("Note 1B example note", "Note 2B example note", "Note 3B example note");

console.log(notes[0].note1); // "Note 1A example note"

// WHERE THE PROBLEM IS...
var noteNum = 2;

console.log(notes[0].note + noteNum); // This SHOULD be "Note 2A example note" but it doesn't work

Fiddle: http://jsfiddle.net/baUaL/

4

2 に答える 2

1

次のようなことを試してください:

console.log(notes[0]["note" + noteNum]);

現在、未定義のフィールド「メモ」にアクセスしようとしています。

于 2013-09-22T21:30:39.757 に答える
0

Try console.log( notes[0]["note"+noteNum] )

于 2013-09-22T21:29:55.203 に答える