英文:
a way to combine/merge objects in json
问题
我有这种不方便的数据输出,如下所示:
[
{
"object_a": {
"1": "some value of 1",
"2": "some value of 2",
"3": ""
},
"obejct_b": {
"1": "",
"2": "",
"3": "some value of 3"
},
..一些其他对象..
}
{
"object_a": {
"1": "some value of 1",
"2": "",
"3": "some value of 3"
},
"obejct_b": {
"1": "",
"2": "some value of 2",
"3": ""
},
..一些其他对象..
}
{
"object_a": {
"1": "",
"2": "some value of 2",
"3": ""
},
"obejct_b": {
"3": "some value of 3",
"1": "some value of 1",
"2": ""
},
..一些其他对象..
}
]
我不知道还要添加什么。它看起来就像它现在这样。不幸的是,我无法更改输出格式。问题在于一些数据在"object_a"中,一些在"object_b"中。
期望的输出应该是这样的:
[
{
"object": {
"1": "some value of 1",
"2": "some value of 2",
"3": "some value of 3"
}
..一些其他对象..
}
{
"object": {
"1": "some value of 1",
"2": "some value of 2",
"3": "some value of 3"
}
..一些其他对象..
}
{
"object": {
"2": "some value of 2",
"3": "some value of 3",
"1": "some value of 1"
}
..一些其他对象..
}
]
我找不到一种使用jQuery来合并两个对象的方法。
英文:
I have this inconvenient output of data, like this:
[
{
"object_a": {
"1": "some value of 1",
"2": "some value of 2",
"3": ""
},
"obejct_b": {
"1": "",
"2": "",
"3": "some value of 3"
},
..some other objects..
}
{
"object_a": {
"1": "some value of 1",
"2": "",
"3": "some value of 3"
},
"obejct_b": {
"1": "",
"2": "some value of 2",
"3": ""
},
..some other objects..
}
{
"object_a": {
"1": "",
"2": "some value of 2",
"3": ""
},
"obejct_b": {
"3": "some value of 3",
"1": "some value of 1",
"2": ""
},
..some other objects..
}
]
I don't know what more to add. It looks straightforward as it is.
Unfortunately, I cannot change the output format.
The problem is that some data is in "object_a" and some in "object_b"
The desired output would be like:
[
{
"object": {
"1": "some value of 1",
"2": "some value of 2",
"3": "some value of 3"
}
..some other objects..
}
{
"object": {
"1": "some value of 1",
"2": "some value of 2",
"3": "some value of 3"
},
..some other objects..
}
{
"object": {
"2": "some value of 2",
"3": "some value of 3",
"1": "some value of 1",
},
..some other objects..
}
]
I couldn't find a way to combine/merge two objects together, with jQuery.
Thanks.
答案1
得分: 0
以下是翻译好的代码部分,不包含其他内容:
const output = [
{
"object_a": {
"1": "some value of 1",
"2": "some value of 2",
"3": ""
},
"obejct_b": {
"1": "",
"2": "",
"3": "some value of 3"
},
"some other object": {
"foo": "bar"
}
},
{
"object_a": {
"1": "some value of 1",
"2": "",
"3": "some value of 3"
},
"obejct_b": {
"1": "",
"2": "some value of 2",
"3": ""
}
},
{
"object_a": {
"1": "",
"2": "some value of 2",
"3": ""
},
"obejct_b": {
"3": "some value of 3",
"1": "some value of 1",
"2": ""
}
}
];
const merged = output.map(item => {
// assumes object_a and obejct_b have the same keys
const mergedAB = Object.keys(item.object_a).reduce((acc, key) => {
// assuming typo in "obejct_b" is intentional
acc[key] = item.object_a[key] || item.obejct_b[key];
return acc;
});
const others = { ...item };
delete others.object_a;
delete others.obejct_b;
return {
object: mergedAB,
...others
}
});
document.body.innerHTML = `<pre>${JSON.stringify(merged, null, 2)}</pre>`;
注意:代码中有一个拼写错误 "obejct_b" 应该是 "object_b",请根据需要进行更正。
英文:
Some assumptions must be made:
- Every object in the top-level array will have the same two (and only these two) properties
object_a
andobejct_b
[sic]. - Every
object_a
andobejct_b
will have the same set of properties. - For any key in an
object_a
that is an empty string, the value in the "merged" result should be that of the correspondingobejct_b
.
With these assumptions, I would use the following code.
(Note that I have left as is the typo in obejct_b
.)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const output = [
{
"object_a": {
"1": "some value of 1",
"2": "some value of 2",
"3": ""
},
"obejct_b": {
"1": "",
"2": "",
"3": "some value of 3"
},
"some other object": {
"foo" : "bar"
}
},
{
"object_a": {
"1": "some value of 1",
"2": "",
"3": "some value of 3"
},
"obejct_b": {
"1": "",
"2": "some value of 2",
"3": ""
}
},
{
"object_a": {
"1": "",
"2": "some value of 2",
"3": ""
},
"obejct_b": {
"3": "some value of 3",
"1": "some value of 1",
"2": ""
}
}
];
const merged = output.map(item => {
// assumes object_a and obejct_b have the same keys
const mergedAB = Object.keys(item.object_a).reduce((acc, key) => {
// assuming typo in "obejct_b" is intentional
acc[key] = item.object_a[key] || item.obejct_b[key];
return acc;
}, {});
const others = { ...item };
delete others.object_a;
delete others.obejct_b;
return {
object: mergedAB,
...others
}
});
document.body.innerHTML = `<pre>${JSON.stringify(merged, null, 2)}</pre>`;
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论