[英]Javascript merge two objects
I am looking for a javascript function
like PHP array_merge_recursive()
. 我正在寻找一个像PHP
array_merge_recursive()
这样的javascript function
。 From what I've read, there is no such thing in javascript, and I want to know how I can implement something similar. 根据我的阅读,javascript中没有这种东西,我想知道如何实现类似的东西。
The problem: 问题:
I have two objects
: 我有两个
objects
:
var obj1 = {
"AUDCAD": {
"t": [1439238600, 1439238600],
"o": [0.9646, 0.9646],
"h": [0.9646, 0.9646],
"l": [0.9645, 0.9645],
"c": [0.9645, 0.9645],
"v": [15, 15]
},
"AUDCHF": {
"t": [1439238600, 1439238600],
"o": [0.7291, 0.7291],
"h": [0.7292, 0.7292],
"l": [0.729, 0.729],
"c": [0.7291, 0.7291],
"v": [6, 6]
}
};
var obj2 = {
"AUDCAD": {
"t": [111],
"o": [111],
"h": [111],
"l": [111],
"c": [111],
"v": [111]
},
"AUDCHF": {
"t": [111],
"o": [111],
"h": [111],
"l": [111],
"c": [111],
"v": [111]
}
};
Of those objects
I need to create a new one like this: 在这些
objects
我需要创建一个新的objects
如下所示:
var obj3 = {
"AUDCAD": {
"t": [1439238600, 1439238600, 111], //Note that the value "111" is added from obj2 to the end of obj1.
"o": [0.9646, 0.9646, 111],
"h": [0.9646, 0.9646, 111],
"l": [0.9645, 0.9645, 111],
"c": [0.9645, 0.9645, 111],
"v": [15, 15, 111]
},
"AUDCHF": {
"t": [1439238600, 1439238600, 111],
"o": [0.7291, 0.7291, 111],
"h": [0.7292, 0.7292, 111],
"l": [0.729, 0.729, 111],
"c": [0.7291, 0.7291, 111],
"v": [6, 6, 111]
}
}
What im doing: 我在做什么:
/*
Code seen here:
http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically
Author: Scdev
*/
function extendObjects() {
var newObject = {};
var overwriteValues = false;
var overwriteObjects = false;
for (var indexArgument = 0; indexArgument < arguments.length; indexArgument++) {
if (typeof arguments[indexArgument] !== 'object') {
if (arguments[indexArgument] == 'overwriteValues_True') {
overwriteValues = true;
} else if (arguments[indexArgument] == 'overwriteValues_False') {
overwriteValues = false;
} else if (arguments[indexArgument] == 'overwriteObjects_True') {
overwriteObjects = true;
} else if (arguments[indexArgument] == 'overwriteObjects_False') {
overwriteObjects = false;
}
} else {
extendObject(arguments[indexArgument], newObject, overwriteValues, overwriteObjects);
}
}
function extendObject(object, extendedObject, overwriteValues, overwriteObjects) {
for (var indexObject in object) {
if (typeof object[indexObject] === 'object') {
if (typeof extendedObject[indexObject] === "undefined" || overwriteObjects) {
extendedObject[indexObject] = object[indexObject];
}
extendObject(object[indexObject], extendedObject[indexObject], overwriteValues, overwriteObjects);
} else {
if (typeof extendedObject[indexObject] === "undefined" || overwriteValues) {
extendedObject[indexObject] = object[indexObject];
}
}
}
return extendedObject;
}
return newObject;
}
var newExtendedObject = extendObjects('overwriteValues_False', 'overwriteObjects_False', obj1, obj2);
I am using nodejs and I have tried without success with some libraries, I've also read some similar questions here but not found a solution to my problem. 我正在使用nodejs,并且尝试使用某些库未成功,但我在这里也阅读了一些类似的问题,但未找到解决问题的方法。 Any suggestions on how I can solve?
关于如何解决的任何建议? Thanks everyone for your time.
谢谢大家的宝贵时间。
Temporal solution 时间解
Temporarily solved my problem with array.push()
临时解决了我的问题与
array.push()
//Pseudo-code
obj1[0]["AUDCAD"]["t"].push(obj2[0]["AUDCAD"]["t"]);
This function assumes that all the keys and sub-keys in object x
are in object y
and vice versa. 此函数假定对象
x
中的所有键和子键都在对象y
,反之亦然。
function merge(x, y) {
var result = {};
Object.keys(x).forEach(
function (k) {
result[k] = {};
Object.keys(x[k]).forEach(
function (j) {
if (j in y[k]) {
result[k][j] = x[k][j].concat(y[k][j]);
}
});
});
return result;
}
Test run: 测试运行:
merge(obj1, obj2);
=> { AUDCAD:
{ t: [ 1439238600, 1439238600, 111 ],
o: [ 0.9646, 0.9646, 111 ],
h: [ 0.9646, 0.9646, 111 ],
l: [ 0.9645, 0.9645, 111 ],
c: [ 0.9645, 0.9645, 111 ],
v: [ 15, 15, 111 ] },
AUDCHF:
{ t: [ 1439238600, 1439238600, 111 ],
o: [ 0.7291, 0.7291, 111 ],
h: [ 0.7292, 0.7292, 111 ],
l: [ 0.729, 0.729, 111 ],
c: [ 0.7291, 0.7291, 111 ],
v: [ 6, 6, 111 ] } }
Object.prototype.merge = function(obj) {
if (typeof this !== 'object'
|| obj === undefined
|| typeof obj !== 'object')
return;
var self = this,
arr = {},
i;
for (i in self) {
if (typeof obj[i] !== 'undefined'
&& typeof self[i] === typeof obj[i]) {
self[i].merge(obj[i]);
arr[i] = true;
}
}
for (i in obj) {
if (!arr[i])
self[i] = obj[i];
}
};
Array.prototype.merge = function(obj) {
if (typeof obj !== 'undefined'
&& !(obj instanceof Array))
return;
var self = this,
i;
for (i = 0; i < obj.length; i++)
self.push(obj[i]);
};
To run 跑步
obj1.merge(obj2);
The data will be appended to obj1 数据将附加到obj1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.