Posted in Javascript onOctober 10, 2017
本文实例讲述了JS实现合并json对象的方法。分享给大家供大家参考,具体如下:
一、问题:
求json对象合并的方法
var a ={"a":"1","b":"2"} var b ={"c":"3","d":"4","e":"5"}
想得到结果:
var c ={"a":"1","b":"2","c":"3","d":"4","e":"5"}
二、实现代码:
<script> function extend(des, src, override){ if(src instanceof Array){ for(var i = 0, len = src.length; i < len; i++) extend(des, src[i], override); } for( var i in src){ if(override || !(i in des)){ des[i] = src[i]; } } return des; } var a ={"a":"1","b":"2"} var b ={"c":"3","d":"4","e":"5"} var c = extend({}, [a,b]); console.log(c); </script>
运行结果:
JS实现合并json对象的方法
- Author -
JEECG开源社区声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@