合并JSON对象的方法

huangapple go评论117阅读模式
英文:

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 and obejct_b [sic].
  • Every object_a and obejct_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 corresponding obejct_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 = [
{
&quot;object_a&quot;: {
&quot;1&quot;: &quot;some value of 1&quot;,
&quot;2&quot;: &quot;some value of 2&quot;,
&quot;3&quot;: &quot;&quot;
},
&quot;obejct_b&quot;: {
&quot;1&quot;: &quot;&quot;,
&quot;2&quot;: &quot;&quot;,
&quot;3&quot;: &quot;some value of 3&quot;
},
&quot;some other object&quot;: {
&quot;foo&quot; : &quot;bar&quot;
}
},
{
&quot;object_a&quot;: {
&quot;1&quot;: &quot;some value of 1&quot;,
&quot;2&quot;: &quot;&quot;,
&quot;3&quot;: &quot;some value of 3&quot;
},
&quot;obejct_b&quot;: {
&quot;1&quot;: &quot;&quot;,
&quot;2&quot;: &quot;some value of 2&quot;,
&quot;3&quot;: &quot;&quot;
}
},
{
&quot;object_a&quot;: {
&quot;1&quot;: &quot;&quot;,
&quot;2&quot;: &quot;some value of 2&quot;,
&quot;3&quot;: &quot;&quot;
},
&quot;obejct_b&quot;: {
&quot;3&quot;: &quot;some value of 3&quot;,
&quot;1&quot;: &quot;some value of 1&quot;,
&quot;2&quot;: &quot;&quot;
}
}
];
const merged = output.map(item =&gt; {
// assumes object_a and obejct_b have the same keys
const mergedAB =  Object.keys(item.object_a).reduce((acc, key) =&gt; {
// assuming typo in &quot;obejct_b&quot; 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 = `&lt;pre&gt;${JSON.stringify(merged, null, 2)}&lt;/pre&gt;`;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 05:02:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定