迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 >

在 Javasript 中如何将对象进行合并

作者:迹忆客 最近更新:2023/03/10 浏览次数:

对象是一种非原始数据类型,它允许我们以键值对的形式存储数据。

在本教程中,我们将使用不同的方法在 JavaScript 中合并两个或多个对象。

在 JavaScript 中,assign() 方法可以迭代地从一个或多个对象读取属性到目标对象。它返回目标对象。我们可以用一个空对象传递两个对象来合并它们。

参考下面的代码。

var obj1 = {
  fruits: ['Banana', 'Mango'],
  vegetables: ['Potato', 'Broccoli'],
};
var obj2 = {
  store: 'Walmart',
};

var obj3 = Object.assign({}, obj1, obj2);
console.log(obj3);

输出:

{fruits: ["Banana", "Mango"], vegetables: ["Potato", "Broccoli"], store: "Walmart"}

在 JavaScript 中,展开运算符 (…) 可以解包数组的所有元素。我们也可以用它来合并对象。

有关如何使用此方法的信息,请参考以下代码。

var obj1 = {
  fruits: ['Banana', 'Mango'],
  vegetables: ['Potato', 'Broccoli'],
};
var obj2 = {
  store: 'Walmart',
};

var obj3 = {...obj1, ...obj2};
console.log(obj3);

输出:

{fruits: ["Banana", "Mango"], vegetables: ["Potato", "Broccoli"], store: "Walmart"}

在这里,我们使用了 array.reduce() 函数,该函数用于在数组中的每个元素上实现一个 reducer 函数(由你提供)。它返回单个输出值。

我们在以下代码片段中实现了这一点。

var obj1 = {
  fruits: ['Banana', 'Mango'],
  vegetables: ['Potato', 'Broccoli'],
};
var obj2 = {
  store: 'Walmart',
};

function merge(...arr){
  return arr.reduce((acc, val) => {
    return { ...acc, ...val  };
  }, {});
}

var obj3 = merge(obj1, obj2);
console.log(obj3);

输出:

{fruits: ["Banana", "Mango"], vegetables: ["Potato", "Broccoli"], store: "Walmart"}

jQuery 是一个轻量级且速度非常快的 JavaScript 库。它简化了 JavaScript 的使用。

extend() 是一种 jQuery 方法,用于将两个或多个对象合并为一个对象。它返回一个对象。
例如,

var obj1 = {
  fruits: ['Banana', 'Mango'],
  vegetables: ['Potato', 'Broccoli'],
};

var obj2 = {
  store: 'Walmart',
};

jQuery.extend(obj1, obj2);

输出:

{fruits: ["Banana", "Mango"], vegetables: ["Potato", "Broccoli"], store: "Walmart"}

在这里,我们创建了自己的函数来返回一个合并的对象。它使用两个对象的属性合并成第三个对象。

参考下面的代码。

var obj1 = {
  fruits: ['Banana', 'Mango'],
  vegetables: ['Potato', 'Broccoli'],
};

var obj2 = {
  store: 'Walmart',
};

function merge_options(obj1,obj2) {
  var obj3 = {};
  for (var key in obj1) { obj3[key] = obj1[key]; }
  for (var key in obj2) { obj3[key] = obj2[key]; }
  return obj3;
};

merge_options(obj1, obj2);

输出:

{fruits: ["Banana", "Mango"], vegetables: ["Potato", "Broccoli"], store: "Walmart"}

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便