浅克隆一个 Map 或 Set

如何在 JavaScript 中浅克隆 地图预备对象?

我想得到一个新的地图或集具有相同的键和值。

55182 次浏览

使用构造函数克隆 Maps 和 Set:

var clonedMap = new Map(originalMap);


var clonedSet = new Set(originalSet);

通过 for 循环创建新 Set 比 Set 构造函数快。地图也是如此,尽管程度较轻。

const timeInLoop = (desc, loopCount, fn) => {
const d = `${desc}: ${loopCount.toExponential()}`
console.time(d)
for (let i = 0; i < loopCount; i++) {
fn()
}
console.timeEnd(d)
}


const set = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])


const setFromForLoop = x => {
const y = new Set()
for (const item of x) y.add(item)
return y
}


const map = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]])


const mapFromForLoop = x => {
const y = new Map()
for (const entry of x) y.set(...entry)
return y
}


timeInLoop('new Set(set)', 1e5, () => new Set(set))


timeInLoop('setFromForLoop(set)', 1e5, () => setFromForLoop(set))


timeInLoop('new Map(map)', 1e5, () => new Map(map))


timeInLoop('mapFromForLoop(map)', 1e5, () => mapFromForLoop(map))

我需要复制(“克隆”)一个 JavaScript 集合,这个问题引起了我对克隆副本的完整性的兴趣,以及对这些副本的源代码的干扰。

测试

var fruit = new Set(['apples', 'bananas']);
var provinces = new Set(['british columbia', 'nova scotia']);
console.log('fruit:', fruit)
console.log('provinces:', provinces)
// fruit: Set [ "apples", "bananas" ]
// provinces: Set [ "british columbia", "nova scotia" ]


// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#implementing_basic_set_operations
function unionSets(setA, setB) {
let _union = new Set(setA);
for (let elem of setB) {
_union.add(elem)
}
return _union;
}


myUnionSet = unionSets(fruit, provinces);
console.log('myUnionSet:', myUnionSet)
// myUnionSet: Set(4) [ "apples", "bananas", "british columbia", "nova scotia" ]


// Tests:
fruit.delete('apples');
provinces.delete('british columbia');


console.log('fruit:', fruit)
console.log('provinces:', provinces)
console.log('myUnionSet:', myUnionSet)
// fruit: Set [ "bananas" ]
// provinces: Set [ "nova scotia" ]
// myUnionSet: Set(4) [ "apples", "bananas", "british columbia", "nova scotia" ]

如果 Map 包含非嵌套结构(例如 Map of Array) ,这里有一个快速深度复制它的方法,可供感兴趣的人使用:

const newMap = new Map();
old.forEach((val, key) => newMap.set(key, [...old.get(key)]));

浅克隆:

var clonedMap = new Map(originalMap)


var clonedSet = new Set(originalSet)

深度克隆:

var deepClonedMap = new Map(JSON.parse(JSON.stringify([...originalMap])))
var deepClonedSet = new Set(JSON.parse(JSON.stringify([...originalSet])))

let originalMap = new Map()
let data = {a:'a',b:'b'}
originalMap.set(1,data)


let shallowCloned = new Map(originalMap)
let deepCloned = new Map(JSON.parse(JSON.stringify([...originalMap])))
data.a = 'p'
console.log('originalMap:',[...originalMap])
console.log('shallowCloned:',[...shallowCloned])
console.log('deepCloned:',[...deepCloned])

这种方法具有浅表复制所需的最小代码量,并且可以完成这项工作。

披露 : 我没有测试这种方式是否会影响性能,也许在大的集合或者你有很多这样的集合,这可能不是最好的方法。.

const mySet = new Set([1, 2, 3, 4]);
const myCloneSet = new Set(Array.from(mySet));
console.log(mySet === myCloneSet) //false