也许有些人不想使用jquery的$.extend()的方法或者项目中没有使用到jquery的,那么下面就给大家展示一种用原生写的合并对象的方法。(虽然方法是在别人上“借”来的,但是就当记录一下吧!)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js合并两个对象</title>
<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","c":"3","f":"6"}
var b ={"d":"4","e":"5","f":"6"}
var c = extend({"h":"7"},[a,b]);console.log(c);
</script>
</head><body>
</body>
</html>