简体   繁体   English

使用 new Map() 映射单个键多个值 - javascript

[英]Map single key multiple values using new Map() - javascript

I need to map single key with multiple values using Map().我需要使用 Map() 将单个键映射到多个值。 But my values keep on getting overwritten.但我的价值观不断被覆盖。 Help.帮助。

 var myMap = new Map(); myMap.set("1","A"); myMap.set("1","B");

 var myMap = new Map();

 myMap.set("1",["A"]);
 myMap.set("1",["B", ...myMap.get('1')]);

You could take a Set for multiple values for a key of a map.您可以为地图的键Set多个值。

 function setValue(map, key, value) { if (!map.has(key)) { map.set(key, new Set(value)); return; } map.get(key).add(value); } var myMap = new Map(); setValue(myMap, "1", "A"); setValue(myMap, "1", "B"); console.log(Array.from(myMap.entries(), ([k, v]) => [k, [...v]]));

A Map data structure is a key/value store. Map数据结构是一个键/值存储。 A single key maps to a single value.单个键映射到单个值。 So you must change the value if you want multiple:因此,如果您想要多个,则必须更改该值:

var myMap = new Map();

myMap.set("1",["A","B"]);

Use an array of values, like above, in order to have a Map use a the key "1" to map to multiple value.像上面一样使用一个值数组,以便让Map使用键"1"映射到多个值。

The second call to set in the OP, will overwrite the first value.在 OP 中set的第二次调用将覆盖第一个值。

You probably want the value to be an array.您可能希望该值是一个数组。 If you want to alter the value one at a time like you example, you need to get the value and then alter it.如果您想像示例一样一次更改一个值,则需要获取该值然后更改它。

 var myMap = new Map(); myMap.set("1", []) //<-- '1' is now an empty array myMap.get("1").push("A"); // <-- now add to the array myMap.get("1").push("B"); console.log(myMap.get("1")) console.log(myMap.get("1")[1]) // <-- just 'B'

Of course, there are lot of other ways of getting the values into the map.当然,还有很多其他方法可以将值放入地图。

  1. Starting with a map object m I iterate over all my key, value pairs.从映射对象m开始,我遍历所有键值对。
  2. For each value, I also append items already stored in the map object under the same key.对于每个值,我还将已存储在地图对象中的项目附加到同一键下。 m.set([value ... m.get(key)])
  3. If these do not exist, and return undefined, I then replace them with with a blank array m.get(key) || []如果这些不存在,并返回未定义,然后我将它们替换为一个空白数组m.get(key) || [] m.get(key) || []
const keyval = [[2,3],[3,5],[3,8]]

m =  new Map(); keyval.forEach(d=> m.set(d[0],[d[1],...m.get(d[0])||[]]));

returns:返回:

Map(2) {2 => Array(1), 3 => Array(2)}

You have two options.你有两个选择。 Define an array for property:为属性定义一个数组:

let myObj = {key1: ["value1", "value2"]};

or assign them dynamically:或动态分配它们:

let myObj = {key1: "value2"};
myObj.key2 = ["value1", "value2"];

You can't do this in JavaScript until you have a unique key in that particular object.除非您在该特定对象中拥有唯一键,否则您无法在 JavaScript 中执行此操作。

Valid:有效的:

 {
    a: "1a23",
    b: {
        a: "23gh"
    },
    c: {
        a: 34
    }
 }

Not Valid:-无效:-

{
    a: "1a23",
    a: "4354" //Not valid
    b: {
        a: "23gh",
        a: "sdfd" //Not valid
    },
    c: {
        a: 34
    }
 }

Cause if you try to get the property value of "a" how does the compiler figures out which is the one you are requesting.因为如果您尝试获取“a”的属性值,编译器如何确定您请求的是哪个。 hence java Script overwrites if you have same property or if you try manually assigning it like this因此如果你有相同的属性或者你尝试像这样手动分配它,java脚本会覆盖

let k = {
a: 1,
a:2
}

It will throw an error.它会抛出一个错误。 so alternatively you can use an array to do this something like因此,或者您可以使用数组来执行此操作,例如

let k = {
a: [1,2]
}
let k2 = myMap.set("a",[1,2]);

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

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