如何创建字符串数组(使用字符串键)?

时间:2016-05-20 22:17:09

标签: javascript

将此PHP脚本转换为Javascript:

Area()

结果将是:

$ar = array();

// add dynamically new element to string array
$ar['aaa'] = "Hello";
$ar['bbb'] = "World";
$ar['ccc'] = "Test!";

foreach($ar as $key => $val){
   print $key." => ".$val."\n";
}

谢谢!

1 个答案:

答案 0 :(得分:1)

使用对象而不是数组:

const obj = {}

obj.aaa = "Hello"
obj.bbb = "World"
obj.ccc = "Test!"

for (const property in obj) {
  console.log(`${property} => ${obj[property]}`)
}