在 JavaScript 中创建关联数组?

javascriptweb developmentobject oriented programming更新于 2025/11/14 3:24:17

您可以使用具有键和值对的对象数组在 JavaScript 中创建关联数组。

关联数组基本上是 JavaScript 中的对象,其中索引被用户定义的键替换。

示例

var customerDetails=
[
   {
      "customerId":"customer-1",
      "customerName":"David",
      "customerCountryName":"US"
   },
   {
      "customerId":"customer-2",
      "customerName":"Bob",
      "customerCountryName":"UK"
   },
   {
      "customerId":"customer-3",
      "customerName":"Carol",
      "customerCountryName":"AUS"
   }
]
for(var i=0;i<customerDetails.length;i++){
   console.log(customerDetails[i].customerName);
}

要运行上述程序,您需要使用以下命令 −

node fileName.js.

这里,文件名是 demo115.js。

输出

这将产生以下输出 −

PS C:\Users\Amit\JavaScript-code> node demo115.js
David
Bob
Carol

相关文章


有用资源