代码拉取完成,页面将自动刷新
<p>Write a function that accepts two deeply nested objects or arrays <code>obj1</code> and <code>obj2</code> and returns a new object representing their differences.</p>
<p>The function should compare the properties of the two objects and identify any changes. The returned object should only contains keys where the value is different from <code>obj1</code> to <code>obj2</code>. For each changed key, the value should be represented as an array <code>[obj1 value, obj2 value]</code>. Keys that exist in one object but not in the other should not be included in the returned object. When comparing two arrays, the indices of the arrays are considered to be their keys. The end result should be a deeply nested object where each leaf value is a difference array.</p>
<p>You may assume that both objects are the output of <code>JSON.parse</code>.</p>
<p> </p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong>
obj1 = {}
obj2 = {
"a": 1,
"b": 2
}
<strong>Output:</strong> {}
<strong>Explanation:</strong> There were no modifications made to obj1. New keys "a" and "b" appear in obj2, but keys that are added or removed should be ignored.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong>
obj1 = {
"a": 1,
"v": 3,
"x": [],
"z": {
"a": null
}
}
obj2 = {
"a": 2,
"v": 4,
"x": [],
"z": {
"a": 2
}
}
<strong>Output:</strong>
{
"a": [1, 2],
"v": [3, 4],
"z": {
"a": [null, 2]
}
}
<strong>Explanation:</strong> The keys "a", "v", and "z" all had changes applied. "a" was chnaged from 1 to 2. "v" was changed from 3 to 4. "z" had a change applied to a child object. "z.a" was changed from null to 2.
</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong>
obj1 = {
"a": 5,
"v": 6,
"z": [1, 2, 4, [2, 5, 7]]
}
obj2 = {
"a": 5,
"v": 7,
"z": [1, 2, 3, [1]]
}
<strong>Output:</strong>
{
"v": [6, 7],
"z": {
"2": [4, 3],
"3": {
"0": [2, 1]
}
}
}
<strong>Explanation:</strong> In obj1 and obj2, the keys "v" and "z" have different assigned values. "a" is ignored because the value is unchanged. In the key "z", there is a nested array. Arrays are treated like objects where the indices are keys. There were two alterations to the the array: z[2] and z[3][0]. z[0] and z[1] were unchanged and thus not included. z[3][1] and z[3][2] were removed and thus not included.
</pre>
<p><strong>Example 4:</strong></p>
<pre>
<strong>Input:</strong>
obj1 = {
"a": {"b": 1},
}
obj2 = {
"a": [5],
}
<strong>Output:</strong>
{
"a": [{"b": 1}, [5]]
}
<strong>Explanation:</strong> The key "a" exists in both objects. Since the two associated values have different types, they are placed in the difference array.</pre>
<p><strong>Example 5:</strong></p>
<pre>
<strong>Input:</strong>
obj1 = {
"a": [1, 2, {}],
"b": false
}
obj2 = {
"b": false,
"a": [1, 2, {}]
}
<strong>Output:</strong>
{}
<strong>Explanation:</strong> Apart from a different ordering of keys, the two objects are identical so an empty object is returned.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= JSON.stringify(obj1).length <= 10<sup>4</sup></code></li>
<li><code>2 <= JSON.stringify(obj2).length <= 10<sup>4</sup></code></li>
</ul>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。