0
// Create the hashmap
var animal = {};
// Add keys to the hashmap
animal[‘cat’] = { sound: ‘meow’, age:8 };
animal[‘dog’] = { sound: ‘bark’, age:10 };
animal[‘bird’] = { sound: ‘tweet’, age:2 };
animal[‘cow’] = { sound: ‘moo’, age:5 };
我正在使用上面的代码尝试创建hashmap。我的问题是,如果放置声音和年龄,我将如何使用变量来放置它们。如何使用变量将键添加到JavaScript哈希表?
例如
var one = 'sound';
var two = 'age';
animal['cat'] = {one: 'meow', two: 9};
。另外:使用正确的引号,'“'或' '',而不是'''(你也使用模板文字,但没有需要在上面。) –