java script 深度对象合并


var obj1 = {
    a: {
        h: [50, 1,2,3],
        j: "hehe"

var obj2 = {
    a: {
        k: "KKK",
        h: [100, 2, 6],
        j: "haha"
    b: {
        i: "abc"



    "a": {
        "h": [1,2,3,6,50,100],
        "j": "haha",
        "k": "KKK"
    "b": {
        "i": "abc"





var margeObj = function (obj1, obj2) {
    this.arrayUnique = function(array){
        if (array instanceof Array === false) {
            throw "ARGEMENTS TYPE IS NOT ARRAY";
        array.sort(function (a, b) {return a - b});
        var result = [array[0]];
        for(var i = 1; i < array.length; i++){
            if (result.indexOf(array[i]) === -1) {
        return result;

    this.jsonSortByKeys = function (json) {
        var keys = Object.keys(json);
        var result = {};
        for (var i = 0; i < keys.length; i++) {
            result[keys[i]] = json[keys[i]];
        return result;

    this.checkVarBothType = function (var1, var2) {
        if (var1 instanceof Array && var2 instanceof Array) {
            return "array";
        if (var1 instanceof Array === false && var2 instanceof Array === false && var1 instanceof Object && var2 instanceof Object) {
            return "object";
        if (var1 instanceof Function && var2 instanceof Function) {
            return "function";
        if (var1 instanceof String && var2 instanceof String) {
            return "string";
        if (var1 instanceof Number && var2 instanceof Number) {
            return "number";
        if (var1 instanceof Boolean && var2 instanceof Boolean) {
            return "boolean";
        return "different";

    this.margeObjLoop = function (obj1, obj2) {
        if (checkVarBothType(obj1, obj2) !== 'object') {
            throw  "ARGEMENTS TYPE IS NOT OBJECT";

        var cell1 = jsonSortByKeys(obj1);
        var cell2 = jsonSortByKeys(obj2);

        for (var i in cell2) {
            if (checkVarBothType(cell1[i], cell2[i]) == "array") {
                cell1[i] = arrayUnique(cell1[i].concat(cell2[i]));
            if (checkVarBothType(cell1[i], cell2[i]) == "object") {
                cell1[i] = jsonSortByKeys(margeObjLoop(cell1[i], cell2[i]));
            cell1[i] = cell2[i];

        return jsonSortByKeys(cell1);

    return this.margeObjLoop(obj1, obj2);

“java script 深度对象合并”的32个回复

  1. Its like you read my mind! You seem to know a lot about this, like
    you wrote the book in it or something. I think that you can do with some pics to drive the
    message home a bit, but instead of that, this is magnificent blog.
    An excellent read. I’ll definitely be back.

  2. Hi there! This blog post could not be written any better!
    Looking at this article reminds me of my previous roommate!
    He continually kept talking about this. I’ll send
    this post to him. Fairly certain he’ll have a good read.
    I appreciate you for sharing!

  3. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is fundamental and all. Nevertheless
    think of if you added some great pictures or video clips
    to give your posts more, “pop”! Your content
    is excellent but with images and clips, this blog could
    certainly be one of the greatest in its field. Good blog!

  4. I like the valuable information you supply on your articles.

    I’ll bookmark your blog and take a look at again right
    here frequently. I am relatively sure I will be told lots
    of new stuff proper here! Good luck for the following!


您的电子邮箱地址不会被公开。 必填项已用*标注