如何将对象的元素推入另一个对象? [英] How to push elements of an object into another object?

查看:136
本文介绍了如何将对象的元素推入另一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与数组类似,我们可以使用数组 .push(item)添加新元素。如何使用对象?它可以在对象内部完成吗?喜欢:

Like in arrays we can add new elements by using array.push(item). How to do the same with objects? And can it be done inside the object? Like:

var myObject={apple: "a", orange: "o"};
var anothObject = {lemon: "l", myObject};


推荐答案

你可以使用jQuery的扩展函数: http://api.jquery.com/jquery.extend/

You can use jQuery's extend function: http://api.jquery.com/jquery.extend/

var object1 = {
  apple: 0,
  banana: { weight: 52, price: 100 },
  cherry: 97
};
var object2 = {
  banana: { price: 200 },
  durian: 100
};

// Merge object2 into object1
$.extend( object1, object2 );

这篇关于如何将对象的元素推入另一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆