You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过最简方式实现对象间的PATCH式更新?

Update Object A with Changes from Object B (Preserving Existing Fields)

Alright, let's tackle this problem step by step. The core requirement is to update the original array (o1) using changes from the update array (o2), while keeping existing fields intact and ignoring new fields from the update (we'll prioritize this strict rule first, with a simplified alternative if adding new fields is acceptable).

Key Rules Recap

  • If a field doesn't exist in o2, keep the original value from o1
  • If o2 has a field that o1 doesn't, don't add it to o1 (strict rule we'll follow first)
  • Only update fields that exist in both o1 and o2

Solution Code (JavaScript)

Here's a clean implementation that sticks to your requirements perfectly:

const o1 = [{ key: "A", content: { field1: "sample text A", field2: "another sample text A" } }, { key: "B", content: { field1: "sample text B", field2: "another sample text B" } }];
const o2 = [{ key: "A", content: { field1: "updated sample text A", field3: "added text A" } }];

const o3 = o1.map(originalItem => {
  // Find the matching update entry using the `key` property
  const updateEntry = o2.find(update => update.key === originalItem.key);
  
  // No matching update? Return the original item as-is
  if (!updateEntry) return originalItem;

  // Create a copy of the original content to avoid mutating the original object
  const updatedContent = { ...originalItem.content };
  
  // Only update fields that already exist in the original content
  Object.keys(updateEntry.content).forEach(field => {
    if (field in updatedContent) {
      updatedContent[field] = updateEntry.content[field];
    }
  });

  // Return the updated item (keeping other properties like `key` unchanged)
  return { ...originalItem, content: updatedContent };
});

console.log(o3);

How This Works

  1. Immutable Update with map: We use map to generate a new array instead of modifying the original o1—this is a best practice to avoid unintended side effects.
  2. Match Entries by key: For each item in o1, we look for a corresponding entry in o2 using the shared key property.
  3. Preserve Original Data: We make a copy of the original content object so we don't alter the source data accidentally.
  4. Selective Field Updates: We loop through all fields in the update's content, but only overwrite values if the field already exists in the original content. This ignores new fields like field3 from your example.
  5. Return Updated/Original Items: Items without a matching update are returned unchanged, while matched items get their existing fields updated as needed.

Simplified Version (If Adding New Fields Is Allowed)

If you're okay with adding new fields from o2 to o1 to simplify the code, here's a much shorter approach using object spreading:

const o3 = o1.map(originalItem => {
  const updateEntry = o2.find(update => update.key === originalItem.key);
  return updateEntry 
    ? { ...originalItem, content: { ...originalItem.content, ...updateEntry.content } }
    : originalItem;
});

This will add new fields like field3 to o1's content, but it’s a concise alternative if the "no new fields" rule isn’t strictly required.

内容的提问来源于stack exchange,提问作者Lajtovo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 02:57:35