You could use an object to collect up your properties while replacing duplicates and then expand/flatten that object back to an array. Something like this:
function merge(args) {
args = Array.prototype.slice.call(arguments);
var o = { };
for(var i = 0; i < args.length; ++i)
for(var j = 0; j < args[i].length; ++j)
o[args[i][j].name] = args[i][j].value;
return o;
}
function expand(o) {
var a = [ ];
for(var p in o)
if(o.hasOwnProperty(p))
a.push({ name: p, value: o[p]});
return a;
}
var arr1 = new Array({name: "lang", value: "English"}, {name: "age", value: "18"});
var arr2 = new Array({name : "childs", value: '5'}, {name: "lang", value: "German"});
var arr3 = expand(merge(arr1, arr2));
I don't know if this is the fastest way but it works for any number of input arrays; for example, this:
var a = expand(
merge(
[{name: "lang", value: "English"}, {name: "age", value: "18"}],
[{name: "childs", value: '5'}, {name: "lang", value: "German"}],
[{name: 'lang', value: 'Pancakes'}]
)
);
Gives you the same thing in a
that was in arr3
with "German" replaced by "Pancakes".
This approach does assume that your objects all have the same {name: ..., value: ...}
form of course.
You can see it working here (open your console please): http://jsfiddle.net/ambiguous/UtBbB/