java script 深度对象合并

由于公司最近的某个业务
顺手解决了一下,JS程序员,一直以来避之不及的问题。
那就是对象合并。
以及对象抽取。

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

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

假设现在有这两个,js对象。
我们往往希望把他合并为,我们想要的样子,而不是简单的在第一层进行覆盖插入。

我们希望这个对象合并结果是这样:

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

大家可以在一下试一下

对象1

对象2

以下即是源码:

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) {
                result.push(array[i]);
            }
        }
        return result;
    }

    this.jsonSortByKeys = function (json) {
        var keys = Object.keys(json);
        keys.sort();
        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]));
                continue;
            }
            if (checkVarBothType(cell1[i], cell2[i]) == "object") {
                cell1[i] = jsonSortByKeys(margeObjLoop(cell1[i], cell2[i]));
                continue;
            }
            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!

发表评论

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