AI Native challenges by level

Level 2 · Applied · State and workflows

Apply JSON merge patches

Merge nested objects, delete null fields and replace arrays.

Problem statement

Write function solution(target, patch). Inputs and outputs are JSON values. Do not modify input arrays or objects. External libraries, network access, and asynchronous functions are not supported. 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. Input limits: at most 50,000 items per input array and 1,000 characters per string. Numbers are finite; integers and accumulated integer results stay in the JavaScript safe-integer range. Other types and validity rules follow the statement above.

Example input and output

Example 1

Function arguments

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

Return value

{
  "b": 2,
  "c": 3
}
Example 2

Function arguments

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

Return value

{
  "nested": {
    "a": 3,
    "b": 2
  }
}
Example 3

Function arguments

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

Return value

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

78 / 20,000 characters

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
문의하기

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

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