使用javascript我有幾個變量。從生成的數字中選擇一個變量
var item1 = {
name : 'apple',
color : 'red',
type : 'fruit'
//etc
};
var item2 = {
name : 'rose',
color : 'red',
type : 'plant'
//etc
};
我想選擇基於數字的項目。我想這樣做。
var select;
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
select = Math.floor(Math.random() * 2) + 1;
但是,我無法調用這些項目。我認爲這只是item[select].name
等,但它顯然沒有發生。幫幫我?