JavaScript中的不正确答案是使用`[]`和`set`与`get`来操作Map。

huangapple go评论93阅读模式
英文:

Incorrect answers with [] and set&get of javascript map

问题

我对以下 JavaScript 代码感到惊讶:

  1. var mp1 = new Map();
  2. mp1[1] = 'apple';
  3. mp1[2] = 'bat';
  4. console.log(mp1.size);
  5. console.log(mp1.set(1, 'test'));
  6. console.log('using get', mp1.get(1));
  7. console.log('using[]', mp1[1])
  8. console.log('------------');
  9. var mp2 = new Map();
  10. mp2.set(1, 'match');
  11. mp2.set(2, 'testing');
  12. console.log(mp2.size);
  13. console.log('using []', mp2[1])
  14. console.log('using get', mp2.get(1))

上述代码的执行结果如下:

  1. 0
  2. [object Map]
  3. using get "test"
  4. using[] "apple"
  5. ------------
  6. 2
  7. undefined
  8. "using []" undefined
  9. "using get" "match"

在 mp1 对象中,大小显示为 0,但在 mp2 中,大小正确显示。此外,对于相同的键,[]set & get 返回了不同的答案。有人能够解释这个现象吗?

英文:

I was amazed by the following code in javascript

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. var mp1 = new Map();
  2. mp1[1] = &#39;apple&#39;;
  3. mp1[2] = &#39;bat&#39;;
  4. console.log(mp1.size);
  5. console.log(mp1.set(1, &#39;test&#39;));
  6. console.log(&#39;using get&#39;, mp1.get(1));
  7. console.log(&#39;using[]&#39;, mp1[1])
  8. console.log(&#39;------------&#39;);
  9. var mp2 = new Map();
  10. mp2.set(1, &#39;match&#39;);
  11. mp2.set(2, &#39;testing&#39;);
  12. console.log(mp2.size);
  13. console.log(&#39;using []&#39;, mp2[1])
  14. console.log(&#39;using get&#39;, mp2.get(1))

<!-- end snippet -->

Following result obtained for the above code

  1. 0
  2. [object Map]
  3. using get &quot;test&quot;
  4. using[] &quot;apple&quot;

  1. 2
  2. undefined
  3. &quot;using []&quot; undefined
  4. &quot;using get&quot; &quot;match&quot;

In mp1 object the size is shown as 0 but in mp2 the size is showing correctly. Also for the same key [] and set&get gave different answers. Can someone provide an explanation for this?

答案1

得分: 2

Map不是一个通常的JS对象,你不能像map[key] = value那样向其添加键。你应该使用Map的方法。

当你使用map[key] = val时,你只是向Map的实例添加属性(在JS中,你可以对任何实例这样做,例如,你可以向函数添加属性)。但是你并没有以这种方式向Map的内容添加任何键/值对。

因此,你应该只使用Map::set()来处理Map的内容。

我猜这种误导可能也会发生在使用localStorage后,你可以实际上使用实例属性来处理内容。但是再次强调,不推荐使用localStorage::setItem()

你可以轻松检查你没有向地图添加任何内容:

  1. const map = new Map;
  2. map.test = true;
  3. console.log([...map.entries()]);

希望这可以帮助你理解。

英文:

The Map isn't a usual JS object where you add keys to it like map[key] = value. You should use the Map's methods.

When you map[key] = val you just add property to the Map's instance (you can do that with any instance in JS, for example you can add a property to a function). But you don't add any key/value to the Map's content that way.

So you should use Map::set() only for working with the Map's content.

I guess this misleading also could happen after working with localStorage where you can actually use instance properties to work with the content. But again that's not recommended over localStorage::setItem().

You can easily check that you don't add anything to the map:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const map = new Map;
  2. map.test = true;
  3. console.log([...map.entries()]);

<!-- end snippet -->

答案2

得分: 2

你在使用 Map 时 使用 [] 表示法时 是错误的。

  1. var mp1 = new Map(); // 好的,我们有一个 Map
  2. mp1[1] = 'apple'; // 向 Map 对象添加新属性值为 'apple'
  3. mp1[2] = 'bat'; // 向 Map 对象添加新属性值为 'bat' - 都不是 Map 元素
  4. console.log(mp1.size); // 是的,这个 Map 没有设置任何内容,所以大小是 0
  5. console.log(mp1.set(1, 'test')); // 现在我们正确使用 set 方法
  6. console.log('使用 get', mp1.get(1)); // 还有 get 方法
  7. console.log('使用 []', mp1[1])
  8. console.log('------------');
  9. var mp2 = new Map(); // 是的,我们有一个 Map
  10. mp2.set(1, 'match'); // 设置 1 包含 "match"
  11. mp2.set(2, 'testing'); // 设置 2 包含 "testing"
  12. console.log(mp2.size); // 是的,我们有两个项目
  13. console.log('使用 []', mp2[1]) // 这不是正确的获取方式
  14. console.log('使用 get', mp2.get(1)) // 这更好
  15. // 如果你想使用 [] 访问元素,首先将其展开到一个数组中:
  16. const arr = [...mp2];
  17. console.log("使用数组表示法", arr[1][1])
英文:

You are using the Map incorrectly when you use the [] notation

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. var mp1 = new Map(); // ok we have a map
  2. mp1[1] = &#39;apple&#39;; // adding a new property to the map object value apple
  3. mp1[2] = &#39;bat&#39;; // adding a new property to the map object value bat - neither are Map elements
  4. console.log(mp1.size); // Yep, the map has nothing set so the size is 0
  5. console.log(mp1.set(1, &#39;test&#39;)); // NOW we use the set correctly
  6. console.log(&#39;using get&#39;, mp1.get(1)); // and get too
  7. console.log(&#39;using[]&#39;, mp1[1])
  8. console.log(&#39;------------&#39;);
  9. var mp2 = new Map(); // yep we have a Map
  10. mp2.set(1, &#39;match&#39;); // setting 1 to contain &quot;match&quot;
  11. mp2.set(2, &#39;testing&#39;); // setting 2 to contain &quot;testing&quot;
  12. console.log(mp2.size); // yes we have two items
  13. console.log(&#39;using []&#39;, mp2[1]) // this is not the way to get it
  14. console.log(&#39;using get&#39;, mp2.get(1)) // this is better
  15. // If you want to access the elements using [] then spread to an array first:
  16. const arr = [...mp2];
  17. console.log(&quot;Using array notation&quot;,arr[1][1])

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 18:19:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定