AI Native 分级编程挑战

第2级 · 实战 · 状态与工作流程

实现JSON部分更新

合并嵌套对象、删除空值字段并替换数组。

题目说明

编写 function solution(target, patch)。输入输出均为 JSON 值,不要修改输入数组或对象。不支持外部库、网络访问及异步函数。 target and patch are JSON values. A nonobject patch (including null and arrays) replaces the entire target. For an object patch, start with {} if target is not an object. Delete keys whose patch value is null; recursively apply all other keys. Replace arrays wholesale. Preserve input and treat '__proto__' and 'constructor' as ordinary data keys. Maximum depth 20. 输入范围:每个输入数组最多50,000项,每个字符串最多1,000个字符。数字必须有限,整数及整数累计结果在JavaScript安全整数范围内。其他类型和有效性遵循上述规则。

输入输出示例

示例 1

函数参数

[
  {
    "a": 1,
    "b": 2
  },
  {
    "a": null,
    "c": 3
  }
]

返回值

{
  "b": 2,
  "c": 3
}
示例 2

函数参数

[
  {
    "nested": {
      "a": 1,
      "b": 2
    }
  },
  {
    "nested": {
      "a": 3
    }
  }
]

返回值

{
  "nested": {
    "a": 3,
    "b": 2
  }
}
示例 3

函数参数

[
  {
    "a": true
  },
  {
    "a": false
  }
]

返回值

{
  "a": false
}
Practice has no points, deadline or submission cap. Use AI and search freely. Sign in to run public examples, or verify locally.

64 / 20,000字符

Add your test · optional

Enter a JSON argument array in target, patch order. Your test runs alongside the three examples.

Save both files in one folder and run this command with Node.js. Read the checks field for individual example results.

node verify-json-merge-patch.mjs
문의하기

서비스명: 포트포올
문의는 운영자에게 이메일로 전송됩니다.

이메일과 문의 내용은 답변을 위해 운영자 및 메일 발송 서비스로 전송됩니다. 비밀번호·결제정보·민감한 개인정보는 입력하지 마세요.