jquery 合并两个 json 对象

jQuery.extend( [ deep ], target, object1, [ objectN ] )合并对象到第一个对象    

//deep为boolean类型,其它参数为object类型

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

var object2 = { banana: {price: 200}, durian: 100};

 

实例1:

$.extend(object1, object2); //合并对象,修改第一个对象

object1 === {apple: 0, banana: {price: 200}, cherry: 97, durian: 100} //结果

 

实例2:

var empty = {}

var object = $.extend(empty , object1, object2);  //有target, object1,object2参数

//合并对象到empty中,没有破坏已有的对象(object1)。object===empty

empty === {apple: 0, banana: {price: 200}, cherry: 97, durian: 100}

 

实例3:

$.extend(true, object1, object2); //合并对象,修改第一个对像 deep==true;

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

posted @ 2017-11-10 15:06  Duke-码动青春  阅读(2861)  评论(0编辑  收藏  举报