你去了,它可能需要对存在的处理值进行一些调整,如果第一个是原始值,第二个是对象(我会让它被对象覆盖),因为你的问题中没有明确说明
原始值仅由您传递的函数处理,但您应该通过阅读代码来理解它,
这应该接近你所需要的。
它将所有传递的对象合并为一个对象,(最后一个参数是函数处理相同的原始属性)如果传递了一个函数,它将不会遍历最后一个,如果没有,所有参数将被视为要合并的对象,您可以并且应该稍微调整一下,因为我现在不太关心代码的清洁度。
也许这可以帮助您进入正确的方向
function merge() {
var srt = typeof arguments[arguments.length - 1] == "function" ? arguments[arguments.length - 1] : function (a, b, prop) {
return a[prop] > b[prop] ? a[prop] : b[prop];
};
var merge = {};
for (var i = 0; i < arguments.length - (typeof arguments[arguments.length - 1] == "function" ?1:0); i++)
inner(arguments[i], merge);
function inner(obj, mrg) {
var type = ({}).toString.call(obj);
if (type == "[object Object]") {
if (!mrg || typeof mrg != "object" ) mrg = {}; //Well it depends if you want primitive values to be overwritten by an Object or not
for (var prop in obj)
if (!mrg[prop] && typeof obj[prop] == "object") mrg[prop] = inner(obj[prop]);
else if (mrg[prop] && typeof obj[prop] == "object") mrg[prop] = inner(obj[prop], mrg[prop]);
else if (mrg[prop]) mrg[prop] = srt(mrg, obj, prop);
else mrg[prop] = obj[prop];
} else if (type == "[object Array]") {
if (!mrg) mrg = [];
for (var i = 0; i < obj.length; i++)
if (!mrg[i] && typeof obj[i] == "object") mrg[i] = inner(obj[i]);
else if (mrg[i] && typeof obj[i] == "object") mrg[i] = inner(obj[i], mrg[i]);
else if (mrg[i]) mrg[i] = srt(mrg, obj, i);
else mrg[i] = obj[i];
} else {
// Handle calling of the inner function with a primitive value
}
return mrg;
}
return merge;
}
var json1 = {
"t": {
"o": [{
"name": "tor",
"pair": 1
}],
"i": [{
"name": "tiset",
"pair": 1
}]
}
};
var json2 = {
"t": {
"a": [{
"name": "tapch",
"pair": 1
}],
"i": [{
"name": "tiset",
"pair": 9
}]
}
};
var json3 = merge(json1, json2, {
a: "test"
}, function (a, b, prop) {
if (prop == "pair") return a[prop] > b[prop] ? a[prop] : b[prop]; //If the propertie is pair, return the bigger one
else return b[prop]; //return the to merge Object
});
console.log(json3);
/*{"a": "test", "t": {"a": [{"name": "tapch", "pair": 1}], "i": [{"name": "tiset", "pair": 9}], "o": [{"name": "tor", "pair": 1}]}}*/
这是一个关于JSBin的例子
评论
要仅合并某些属性,您可以添加一个作为属性映射的参数,然后在存在映射时迭代映射属性并仅复制它们。如果没有,请从对象中复制所有属性以进行合并。
所以从代码上看,这可能看起来像这样:
function merge() {
var srt = typeof arguments[arguments.length - 1] == "function" ? arguments[arguments.length - 1] : function (a, b, prop) {
return a[prop] > b[prop] ? a[prop] : b[prop];
};
var merge = {};
var map = arguments[arguments.length - 2]._MAP ? arguments[arguments.length - 2] : false;
delete map._MAP
for (var i = 0; i < arguments.length - (typeof arguments[arguments.length - 1] == "function" ? 1 : 0) - (map ? 1 : 0); i++) {
inner(arguments[i], merge, map);
}
function inner(obj, mrg, map) {
var type = ({}).toString.call(obj);
if (type == "[object Object]") {
if (!mrg || typeof mrg != "object") mrg = {}; //Well it depends if you want primitive values to be overwritten by an Object or not
for (var prop in map || obj) {
if (!mrg[prop] && typeof obj[prop] == "object") mrg[prop] = inner(obj[prop], undefined, map[prop]);
else if (mrg[prop] && typeof obj[prop] == "object") mrg[prop] = inner(obj[prop], mrg[prop], map[prop]);
else if (mrg[prop]) mrg[prop] = srt(mrg, obj, prop);
else if (!obj[prop]) return
else mrg[prop] = obj[prop];
}
} else if (type == "[object Array]") {
if (!mrg) mrg = [];
for (var i = 0; i < (map || obj).length; i++)
if (!mrg[i] && typeof obj[i] == "object") mrg[i] = inner(obj[i], undefined, map[i]);
else if (mrg[i] && typeof obj[i] == "object") mrg[i] = inner(obj[i], mrg[i], map[i]);
else if (mrg[i]) mrg[i] = srt(mrg, obj, i);
else mrg[i] = obj[i];
} else {
// Handle calling of the inner function with a primitive value
}
return mrg;
}
return merge;
}
var json1 = {
"t": {
"o": [{
"name": "tor",
"pair": 1
}],
"i": [{
"name": "tiset",
"pair": 1
}]
}
};
var json2 = {
"t": {
"a": [{
"name": "tapch",
"pair": 1
}, {
a: "a"
}],
"i": [{
"name": "tiset",
"pair": 9
}]
}
};
var json3 = merge(json1, json2, {
_MAP: true,
"t": {
a: [{
name: true
},
false],
i: false
}
}, function (a, b, prop) {
if (prop == "pair") return a[prop] > b[prop] ? a[prop] : b[prop]; //If the propertie is pair, return the bigger one
else return b[prop]; //return the to merge Object
});
var json4 = merge(json1, json2, {
_MAP: true,
"t": {
a: [false],
i: false
}
}, function (a, b, prop) {
if (prop == "pair") return a[prop] > b[prop] ? a[prop] : b[prop]; //If the propertie is pair, return the bigger one
else return b[prop]; //return the to merge Object
});
console.log(json3, json4);
/*{"t": {"a": [{"name": "tapch"}, {"a": "a"}], "i": [{"name": "tiset", "pair": 9}]}}*/
/*{"t": {"a": [{"name": "tapch", "pair": 1}], "i": [{"name": "tiset", "pair": 9}]}}*/
正如你看到的
json3
已创建,传递了应复制以下内容的地图:
json4
已创建,传递了应复制以下内容的地图:
如您所见,尚未复制其他属性
这是另一个JSBin,只需看一下并摆弄一下(并阅读代码)