私はこのコードを見つけました...
var newEntry, table = [];
newEntry = {
id: '321',
price: '1000',
};
table.push(newEntry);
alert(table[0].id);
期待どおりに動作します。ただし、このように複数のエントリを追加する必要があります...
var newFont, newColor, table = [];
newFont = {
family: 'arial',
size: '12',
};
newColor = {
hex: 'red',
};
table.push(newFont);
table.push(newColor);
alert(table[0].font);
問題
- 書きたくない
table[0].family
。 - 代わりに書きたいと思います
table['font'].family
。 - 単なる数字ではなく、名前付きのキーです。設定が増えるとさらに良いです。