将地图复制到现有地图的最有效方式

时间:2019-03-27 13:24:53

标签: javascript dictionary ecmascript-6 clone

我想用下面的代码片段Map a 覆盖Map b 。我目前正在清除 b 的内容,然后逐一设置条目。编写copyMap函数的更有效的方式(更少的垃圾,更快等)是什么?

const a = new Map([[1, 2], [2, 10], [3, 20]]);
const b = new Map([[1,5]]);

// I want to copy a into b, at the end b should look like
// b = new Map([[1, 2], [2, 10], [3, 20]]);

console.log(a,b);

function copyMap(srcMap, destMap) {
  destMap.clear();
  
  // Maybe this loop could be done in a single call somehow?
  for(let item of srcMap) {
    destMap.set(item[0], item[1]);
  }
}
copyMap(a, b);

// a and b should have same entries
console.log('a: ',[...a.entries()], 'b: ', [...b.entries()]);

1 个答案:

答案 0 :(得分:0)

你可以做

modelspace.add_text(label, dxfattribs={'layer': 'TEXTLAYER', 'text_generation_flag': 2, 'height': 4}).set_pos((cx, cy), align='CENTER')

,您需要为b删除const声明