Javascript 对象属性作为 map 的键

标签 javascript

我是基本 JavaScript 概念的新手,所以想了解这是怎么可能的。

我有一个对象x

var x={}

它有一个属性y,其值为一个数组

x.y=["a","b","c"]`

所以它就像一个 map ,其中 y 是键,数组["a","b","c"] 是值。那么如何 y 持有另一个名为 z 的属性,它有一些其他值,例如“hi”

x.y.z= "hi" 

y 已经是 ["a","b","c"]----------------- --------> x.y= ["a","b","c"]

怎么可能{"z":"hi"}那么------------------------>x.y={"z":"hi"}

如果展开x,这就是它的显示方式 enter image description here

为了清楚地了解这一点,我应该熟悉哪些基本概念?

最佳答案

因为arrays是具有一些特殊方法的对象。

Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed. Since an array's size length grow or shrink at any time, JavaScript arrays are not guaranteed to be dense. In general, these are convenient characteristics; but if these features are not desirable for your particular use, you might consider using typed arrays.

Some people think that you shouldn't use an array as an associative array. In any case, you can use plain objects instead, although doing so comes with its own caveats. See the post Lightweight JavaScript dictionaries with arbitrary keys as an example.

关于Javascript 对象属性作为 map 的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35499195/

相关文章:

javascript - 运行 AJAX 脚本

javascript - 创建用户后如何防止自动登录

javascript - 如何将 JSON 字符串转换并执行为带参数的函数?

javascript - 删除字符串中的反斜杠

javascript - jQuery promise .done() 立即触发,不等待服务器返回

javascript - Shadow DOM——封装JS和CSS文件

javascript - 如何从控制台查看 Javascript 文档?

javascript - 创建一个包含特定字符串的数组以及如何将字符串逐个字母地分割

javascript - 以慢动作在页面上运行 javascript?

javascript - 当我在 Flex 4 中滚动 spark Datagrid 时如何阻止浏览器窗口滚动?