gpt4 book ai didi

Javascript合并两个对象

转载 作者:行者123 更新时间:2023-11-30 06:52:49 24 4
gpt4 key购买 nike

我正在寻找类似 PHP array_merge_recursive() 的 javascript 函数。据我所知,javascript 中没有这样的东西,我想知道如何实现类似的东西。

问题:

我有两个对象:

 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]
}
};

对于那些对象,我需要像这样创建一个新对象:

   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]
}
}

我在做什么:

/* 
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);

我正在使用 nodejs,我尝试了一些库但没有成功,我也在这里阅读了一些类似的问题,但没有找到解决我的问题的方法。关于如何解决的任何建议?感谢大家的宝贵时间。

时间解决方案

暂时用array.push()解决了我的问题

//Pseudo-code    
obj1[0]["AUDCAD"]["t"].push(obj2[0]["AUDCAD"]["t"]);

最佳答案

此函数假定对象 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;
}

测试运行:

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 ] } }

关于Javascript合并两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31950201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com