简体   繁体   English

“设置是数组,因为地图是对象”是什么意思?

[英]What does “Sets are to arrays as maps are to objects” mean?

I'm learning about ES6 and stumbled upon this phrase in this video that states: 'you could say that Sets are to Arrays as Maps are to Objects'. 我正在学习ES6,并在这段视频中偶然发现了这句话:'你可以说,集合是阵列,因为地图是对象'。

What does this phrase mean ? 这个短语是什么意思 ? Why is a Set more linked to arrays than maps are ? 为什么Set更多地链接到数组而不是map? (and vice-versa for objects). (反之亦然)。

I know this is a really specific question, but my head is really turning since i've heard this phrase! 我知道这是一个非常具体的问题,但是自从我听到这句话后,我的头脑真的转过来了!

Thank you in advance, i'm new to question on SO so any comment is appreciated. 提前谢谢你,我是SO的新人,所以任何评论都表示赞赏。

A Set is a collection of values, just like an array is a collection of values (no keys involved, except .length / .size ) Set是值的集合,就像数组是值的集合一样(不涉及任何键,除了.length / .size

A Map is a collection of key-value pairs, just like an object is a collection of key-value pairs. Map是键值对的集合,就像对象是键值对的集合一样。 (though the keys of a Map can be anything , not just strings) (虽然Map的键可以是任何东西 ,而不仅仅是字符串)

Of course, there are many more differences, but the distinction between values and key-value pairs is what's most relevant for what you're asking. 当然,还有很多不同之处,但价值观键值对之间的区别与您提出的问题最为相关。

Map and object example: Map和对象示例:

 const key = 'key'; const value = 'value'; const map = new Map(); const obj = {}; map.set(key, value); obj[key] = value; 

Set and array example: Set和数组示例:

 const value = 'value'; const set = new Set(); const arr = []; set.add(value); arr.push(value); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM