jquery 合并两个 json 对象.html

Scroll Down
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}

鲁ICP备 19052590 号

Copyright © 2020 码动青春

Powered by Halo • Theme by Subtle Galaxy • REFERENCE FROM 寒山

本站运行:

感谢位小伙伴的 次访问

Back to the top