<script>
const a = { a: 1, b: 4 }
const b = { b: 2, c: 3 }
// 1.Object.assign合并
console.log(Object.assign(a, b));
// 2.es6扩展运算符
console.log({ ...a, ...b });
// 3.自己封装方法
function extend(target, source) {
for (var obj in source) {
target[obj] = source[obj]
}
return target;
}
console.log(extend(a, b));
</script>
js 合并对象 (3种方法)
最新推荐文章于 2024-01-23 11:08:56 发布